From 4e2b74e10be103354a46e8446d1f4b2c255a660c Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 28 Oct 2021 19:38:50 +0300 Subject: [PATCH 1/2] version: force proper typing on version literals Types should be generic and stay the same across different versions E.g. `preReleaseTag` should be typed `string | null` even if it's a `null` literal for this particular release --- resources/gen-version.js | 12 +++++++----- src/version.ts | 10 +++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/resources/gen-version.js b/resources/gen-version.js index e4219d5cf5..944b51401d 100644 --- a/resources/gen-version.js +++ b/resources/gen-version.js @@ -18,16 +18,18 @@ const body = ` /** * A string containing the version of the GraphQL.js library */ -export const version = '${version}'; +export const version = '${version}' as string; /** * An object containing the components of the GraphQL.js version string */ export const versionInfo = Object.freeze({ - major: ${major}, - minor: ${minor}, - patch: ${patch}, - preReleaseTag: ${preReleaseTag ? `'${preReleaseTag}'` : 'null'}, + major: ${major} as number, + minor: ${minor} as number, + patch: ${patch} as number, + preReleaseTag: ${ + preReleaseTag ? `'${preReleaseTag}'` : 'null' + } as string | null, }); `; diff --git a/src/version.ts b/src/version.ts index 8adb01a97c..971122bc7c 100644 --- a/src/version.ts +++ b/src/version.ts @@ -4,14 +4,14 @@ /** * A string containing the version of the GraphQL.js library */ -export const version = '16.0.0-rc.7'; +export const version = '16.0.0-rc.7' as string; /** * An object containing the components of the GraphQL.js version string */ export const versionInfo = Object.freeze({ - major: 16, - minor: 0, - patch: 0, - preReleaseTag: 'rc.7', + major: 16 as number, + minor: 0 as number, + patch: 0 as number, + preReleaseTag: 'rc.7' as string | null, }); From da23abc6cbde94ea570567ef31f9ed4c539ab1ac Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 28 Oct 2021 20:15:05 +0300 Subject: [PATCH 2/2] build-npm: fix type inference during TS declarations build --- resources/build-npm.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/resources/build-npm.js b/resources/build-npm.js index 5ea2505852..a54cfaa983 100644 --- a/resources/build-npm.js +++ b/resources/build-npm.js @@ -35,16 +35,29 @@ if (require.main === module) { } } - const tsProgram = ts.createProgram(['src/index.ts'], { - ...ts.getDefaultCompilerOptions(), + // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file + const tsConfig = JSON.parse( + fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'), + ); + assert( + tsConfig.compilerOptions, + '"tsconfig.json" should have `compilerOptions`', + ); + const tsOptions = { + ...tsConfig.compilerOptions, + noEmit: false, declaration: true, declarationDir: './npmDist', emitDeclarationOnly: true, - }); + }; - const tsResult = tsProgram.emit(undefined, (filepath, body) => { + const tsHost = ts.createCompilerHost(tsOptions); + tsHost.writeFile = (filepath, body) => { writeGeneratedFile(filepath, body); - }); + }; + + const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost); + const tsResult = tsProgram.emit(); assert( !tsResult.emitSkipped, 'Fail to generate `*.d.ts` files, please run `npm run check`',