Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output only d.ts of my typescript project?

Tags:

typescript

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

like image 322
jaylinwang Avatar asked Sep 14 '18 06:09

jaylinwang


People also ask

What is D ts in TypeScript?

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.

What is index D ts TypeScript?

*. 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.

Where does TypeScript look for D ts files?

The . d. ts file is usually placed adjacent to the . ts file, it is providing the typings for.


1 Answers

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.

like image 194
Matt McCutchen Avatar answered Sep 28 '22 07:09

Matt McCutchen