Skip to content

Commit

Permalink
[babel__traverse] Return correct type in get() when Array is nullable (
Browse files Browse the repository at this point in the history
…DefinitelyTyped#59788)

* fix(babel__traverse): Return correct type in get() when Array is nullable

* fix: Fix return type
  • Loading branch information
danez authored Apr 26, 2022
1 parent de71f8e commit 667cea3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions types/babel__traverse/babel__traverse-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ const visitorWithInvalidDenylist: Visitor = {
denylist: ['SomeRandomType'],
};

const objectTypeAnnotation: NodePath<t.ObjectTypeAnnotation> = new NodePath<t.ObjectTypeAnnotation>(
null as any,
{} as any,
);

objectTypeAnnotation.get('indexers'); // $ExpectType NodePath<null | undefined> | NodePath<ObjectTypeIndexer>[]

// Test that NodePath can be narrowed from union to single type
const path: NodePath<t.ExportDefaultDeclaration | t.ExportNamedDeclaration> = new NodePath<t.ExportNamedDeclaration>(
null as any,
Expand Down
2 changes: 2 additions & 0 deletions types/babel__traverse/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ export class NodePath<T = Node> {
context?: boolean | TraversalContext,
): T[K] extends Array<Node | null | undefined>
? Array<NodePath<T[K][number]>>
: T[K] extends Array<Node | null | undefined> | null | undefined
? Array<NodePath<NonNullable<T[K]>[number]>> | NodePath<null | undefined>
: T[K] extends Node | null | undefined
? NodePath<T[K]>
: never;
Expand Down

0 comments on commit 667cea3

Please sign in to comment.