Skip to content

Commit

Permalink
fix: TS type error on `strictNullChecks: true (#7931)
Browse files Browse the repository at this point in the history
`Partial<T>` includes `{}`, therefore `NewVisitor[keyof NewVisitor]` includes `undefined`, and indexing `undefined` is error.

Issue: #7519
  • Loading branch information
dittos committed Jul 8, 2022
1 parent 73e9f08 commit a4fe500
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .changeset/purple-eyes-fix.md
@@ -0,0 +1,29 @@
---
'@graphql-codegen/plugin-helpers': patch
---

Fix TS type error on strictNullChecks: true

Fix the compiler error:

```
node_modules/@graphql-codegen/plugin-helpers/oldVisit.d.ts:5:75 - error TS2339: Property 'enter' does not exist on type '{ readonly enter?: ASTVisitFn<NameNode> | undefined; readonly leave: ASTReducerFn<NameNode, unknown>; } | { readonly enter?: ASTVisitFn<DocumentNode> | undefined; readonly leave: ASTReducerFn<...>; } | ... 41 more ... | undefined'.
5 enter?: Partial<Record<keyof NewVisitor, NewVisitor[keyof NewVisitor]['enter']>>;
~~~~~~~
node_modules/@graphql-codegen/plugin-helpers/oldVisit.d.ts:6:75 - error TS2339: Property 'leave' does not exist on type '{ readonly enter?: ASTVisitFn<NameNode> | undefined; readonly leave: ASTReducerFn<NameNode, unknown>; } | { readonly enter?: ASTVisitFn<DocumentNode> | undefined; readonly leave: ASTReducerFn<...>; } | ... 41 more ... | undefined'.
6 leave?: Partial<Record<keyof NewVisitor, NewVisitor[keyof NewVisitor]['leave']>>;
~~~~~~~
Found 2 errors in the same file, starting at: node_modules/@graphql-codegen/plugin-helpers/oldVisit.d.ts:5
```

Only happens when TS compiler options `strictNullChecks: true` and `skipLibCheck: false`.

`Partial<T>` includes `{}`, therefore `NewVisitor[keyof NewVisitor]` includes `undefined`, and indexing `undefined` is error.
Eliminate `undefined` by wrapping it inside `NonNullable<...>`.

Related #7519
4 changes: 2 additions & 2 deletions packages/utils/plugins-helpers/src/oldVisit.ts
Expand Up @@ -3,8 +3,8 @@ import { ASTNode, visit } from 'graphql';
type VisitFn = typeof visit;
type NewVisitor = Partial<Parameters<VisitFn>[1]>;
type OldVisitor = {
enter?: Partial<Record<keyof NewVisitor, NewVisitor[keyof NewVisitor]['enter']>>;
leave?: Partial<Record<keyof NewVisitor, NewVisitor[keyof NewVisitor]['leave']>>;
enter?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['enter']>>;
leave?: Partial<Record<keyof NewVisitor, NonNullable<NewVisitor[keyof NewVisitor]>['leave']>>;
} & NewVisitor;

export function oldVisit(
Expand Down

1 comment on commit a4fe500

@vercel
Copy link

@vercel vercel bot commented on a4fe500 Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.