From 2b2f1b8f1a9fe4d7a5e659e1e06d8aafdf09896c Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 28 Oct 2021 20:15:05 +0300 Subject: [PATCH] build-npm: fix type inference during TS declarations build --- resources/build-npm.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/resources/build-npm.js b/resources/build-npm.js index 5ea2505852b..4476298fe53 100644 --- a/resources/build-npm.js +++ b/resources/build-npm.js @@ -35,16 +35,27 @@ 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 = require('../tsconfig.json'); + 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`',