Skip to content

Commit 2831917

Browse files
committed
build-npm: fix type inference during TS declarations build
1 parent d2eb7bd commit 2831917

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

resources/build-npm.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,29 @@ 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 = JSON.parse(
40+
fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'),
41+
);
42+
assert(
43+
tsConfig.compilerOptions,
44+
'"tsconfig.json" should have `compilerOptions`',
45+
);
46+
const tsOptions = {
47+
...tsConfig.compilerOptions,
48+
noEmit: false,
4049
declaration: true,
4150
declarationDir: './npmDist',
4251
emitDeclarationOnly: true,
43-
});
52+
};
4453

45-
const tsResult = tsProgram.emit(undefined, (filepath, body) => {
54+
const tsHost = ts.createCompilerHost(tsOptions);
55+
tsHost.writeFile = (filepath, body) => {
4656
writeGeneratedFile(filepath, body);
47-
});
57+
};
58+
59+
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
60+
const tsResult = tsProgram.emit();
4861
assert(
4962
!tsResult.emitSkipped,
5063
'Fail to generate `*.d.ts` files, please run `npm run check`',

0 commit comments

Comments
 (0)