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

I want to create a single .d.ts file for a single .ts file, without also creating .js files from the source .ts file.

I want to run something like:

 $ tsc myFile.ts -d 

But the result will be only the myFile.ts generated .d.ts file.

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

My goal is es2015 , so the module option should be the default for CommonJS (if that matters).

+5
source share
1 answer

I want to create one .d.ts file for one .ts file, without also creating a .js file from my source .ts file.

While there is no compiler for this parameter, we can contain and delete the resulting .js files.

Case 1: One .ts file that does not import local .ts files.

What we can do: 1. appoint . as the declaration directory, 2. designate temp as the JavaScript directory and 3. delete the temp directory after forwarding.

 tsc --declaration --declarationDir . --outDir temp foo.ts rm -rf temp 

This works if foo.ts does not import other local .ts files. When foo.ts imports other local .ts files, the compiler creates a separate .d.ts file for each. Since this is not what we want right now, it’s better.

Case 2: One .ts file that imports local .ts files

If our use case allows you to create system or amd style declaration files, we can do this:

 tsc foo.ts --outFile foo.js --declaration --module system rm -rf foo.js 

Result in both cases

Both of these approaches generate the same directory structure with different ad file syntax.

 bar bar.ts foo.d.ts <---- a single declaration file foo.ts tsconfig.json 
+2
source

Source: https://habr.com/ru/post/1270601/


All Articles