How do I generate only a .d.ts from a single typescript file?

Viewed 6038

I want to create a single .d.ts file for a single .ts file without also creating a .js file(s) from my source .ts file(s).

I want to run something like:

$ tsc myFile.ts -d

But have the result be only myFile.ts's generated .d.ts file.

Currently, the result is that that file and all of the .ts files in my project produce their own .js and .d.ts files.

My target is es2015, so the module option should be defaulting to CommonJS (if that matters).

2 Answers

Since the last accepted answer, TS has added support for emitDeclarationOnly that does just that

Example tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "types",
    "emitDeclarationOnly": true
  },
  "include": ["src"]
}

Example usage

npx tsc
Related