diff --git a/.changeset/purple-eyes-fix.md b/.changeset/purple-eyes-fix.md new file mode 100644 index 00000000000..603d3647e9b --- /dev/null +++ b/.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 | undefined; readonly leave: ASTReducerFn; } | { readonly enter?: ASTVisitFn | undefined; readonly leave: ASTReducerFn<...>; } | ... 41 more ... | undefined'. + +5 enter?: Partial>; + ~~~~~~~ + +node_modules/@graphql-codegen/plugin-helpers/oldVisit.d.ts:6:75 - error TS2339: Property 'leave' does not exist on type '{ readonly enter?: ASTVisitFn | undefined; readonly leave: ASTReducerFn; } | { readonly enter?: ASTVisitFn | undefined; readonly leave: ASTReducerFn<...>; } | ... 41 more ... | undefined'. + +6 leave?: Partial>; + ~~~~~~~ + + +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` includes `{}`, therefore `NewVisitor[keyof NewVisitor]` includes `undefined`, and indexing `undefined` is error. +Eliminate `undefined` by wrapping it inside `NonNullable<...>`. + +Related #7519 diff --git a/packages/utils/plugins-helpers/src/oldVisit.ts b/packages/utils/plugins-helpers/src/oldVisit.ts index bbe63e12156..93562cc70a3 100644 --- a/packages/utils/plugins-helpers/src/oldVisit.ts +++ b/packages/utils/plugins-helpers/src/oldVisit.ts @@ -3,8 +3,8 @@ import { ASTNode, visit } from 'graphql'; type VisitFn = typeof visit; type NewVisitor = Partial[1]>; type OldVisitor = { - enter?: Partial>; - leave?: Partial>; + enter?: Partial['enter']>>; + leave?: Partial['leave']>>; } & NewVisitor; export function oldVisit(