For my typescript project, want to out d.ts
file for entry file only:
when i have a index.ts:
import a from './a';
import b from './b';
export default {
a,
b
};
when i compile with ts with config tsconfig.json:
{
"compilerOptions": {
"outDir": "./lib",
"declarationDir": "./build",
"moduleResolution": "node",
"strictNullChecks": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"noImplicitAny": true,
"target": "es6",
"declaration": true,
"lib": [
"dom",
"es2015",
"es2016",
],
},
"include": [
"packages/index.ts",
"typings"
],
"exclude": [
"node_modules",
"**/__tests__",
"**/__mocks__",
]
}
would output tree file a.d.ts, b.d.ts, index.d.ts
how only output index.d.ts
d. ts is the type definition files that allow to use existing JavaScript code in TypeScript. For example, we have a simple JavaScript function that calculates the sum of two numbers: // math.js.
*. d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.
The . d. ts file is usually placed adjacent to the . ts file, it is providing the typings for.
You can use a separate tsc
run with outFile
to produce a single .d.ts
file that has a declare module
block for each original module. You could do this by removing the declaration
and declarationDir
options from your original tsconfig.json
and creating a separate tsconfig.declaration.json
with:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "amd", // https://github.com/Microsoft/TypeScript/issues/27117
"declaration": true,
"emitDeclarationOnly": true,
"outFile": "build/index.d.ts"
}
}
The only problem is that TypeScript 3.0.3 incorrectly passes through relative import paths such as ./a
to the output, so for now, you'll need to set up your module resolution options so that you can use non-relative import paths. The problem has been fixed in this pull request, but the fix has not been released as of 2018-09-15.
See this long thread for discussion of other approaches to declaration bundling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With