Skip to content

Commit 2b2f1b8

Browse files
committed
build-npm: fix type inference during TS declarations build
1 parent a27ad0b commit 2b2f1b8

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

resources/build-npm.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,27 @@ if (require.main === module) {
3535
}
3636
}
3737

38-
const tsProgram = ts.createProgram(['src/index.ts'], {
39-
...ts.getDefaultCompilerOptions(),
38+
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
39+
const tsConfig = require('../tsconfig.json');
40+
assert(
41+
tsConfig.compilerOptions,
42+
'"tsconfig.json" should have `compilerOptions`',
43+
);
44+
const tsOptions = {
45+
...tsConfig.compilerOptions,
46+
noEmit: false,
4047
declaration: true,
4148
declarationDir: './npmDist',
4249
emitDeclarationOnly: true,
43-
});
50+
};
4451

45-
const tsResult = tsProgram.emit(undefined, (filepath, body) => {
52+
const tsHost = ts.createCompilerHost(tsOptions);
53+
tsHost.writeFile = (filepath, body) => {
4654
writeGeneratedFile(filepath, body);
47-
});
55+
}
56+
57+
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
58+
const tsResult = tsProgram.emit();
4859
assert(
4960
!tsResult.emitSkipped,
5061
'Fail to generate `*.d.ts` files, please run `npm run check`',

0 commit comments

Comments
 (0)