Within your tsconfig.json
configuration file, you have the ability to establish a compiler option labeled declaration and set it to true
. This setting ensures that when you compile your TypeScript files into JavaScript, the necessary definition files (.d.ts
) are automatically generated. These definition files can then be imported within your TypeScript/JavaScript project using type assertion.
To illustrate:
Take a look at the tsconfig.json
file from my project:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es5",
"experimentalDecorators": true,
"strict": true,
"moduleResolution": "node",
"sourceMap": true,
"noUnusedLocals": true,
"incremental": true,
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsBuildInfoFile",
"baseUrl": ".",
"resolveJsonModule": true,
"declaration": true,
"typeRoots": ["./src/types", "./node_modules/@types"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "test/**/*"]
}