17

I developed a library, which also contains TypeScript files, in Visual Studio 2013. The files are compiling correctly to JS files (AMD).

What I want is to create a single declaration file for this lib, but this does not work:

tsc --declaration --module AMD --out out.d.ts [files.ts]

Can someone please lead me to the right path?

TypeScript 0.9.1.1 is used.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
bobschi
  • 325
  • 1
  • 3
  • 13

4 Answers4

5

Its because you are providing .d.ts to out which will overwrite the declaration file with js contents. Also, you need to remove module amd because that works on per file basis (not --out friendly, see http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1)

So, try:

tsc [files.ts] --declaration --out out.js

which will generate out.d.ts

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 1
    Changed to "tsc --declaration --module AMD --out out.js [files.ts]", which now generates js and d.ts files, BUT seperatly for each source file. – bobschi Oct 30 '13 at 09:02
  • Now I get: 'error TS5037: Cannot compile external modules unless the "--module" flag is provided.' – bobschi Oct 30 '13 at 12:10
  • If your library is designed to use AMD you cannot have it merged to a single js (for which you will use r.js) or single .d.ts (for which there isnt any alternative solution) using tsc – basarat Oct 30 '13 at 21:18
  • `out` is renamed to `outFile` and Only 'amd' and 'system' modules are supported alongside `--outFile`. (2018) – Onur Yıldırım Aug 11 '18 at 03:32
1

You could always do it after the fact with command line calls. In Visual Studio, use the following as a post-build event command line call:

del $(OutDir)combined.d.ts
type $(OutDir)*.d.ts > $(OutDir)combined.d.ts

It might need to be $(ProjectDir)Some\Other\Path\*.d.ts, etc. See here for more

Andy W
  • 229
  • 1
  • 9
  • this will not work, since local imports will not be resolved (ie: `import { plugin1 } from './lib';`) – YoniXw Oct 22 '20 at 23:12
0

In addition to Andy W's answer you will want to remove the reference path lines. Use FIND to do this.

del "$(ProjectDir)bin\combined.d.ts"
type "$(ProjectDir)*.d.ts" "$(ProjectDir)subFolder1\*.d.ts" "$(ProjectDir)subFolder2\*.d.ts" | FIND /V /I "/// <reference path="  > "$(ProjectDir)bin\combined.d.ts"
RockResolve
  • 1,423
  • 21
  • 29
0

Generate one declaration file for Typescript lib.

You don't need to do that (or at least you haven't provided a use case for it).

Your output folder e.g. /module/bar should look like:

  • index.js < generated .js for /module/bar.ts
  • index.d.ts < generated .d.ts for /module/bar.ts

And when users require /module/bar runtime resolves to .js and TypeScript type system resolves to .d.ts automatically.

Examples

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 1
    I've upvoted b/c I liked that library of yours so much as an example, however, a use case could be a library that is bundled into one file prior to packaging, so having all types in a single file could be useful in this case. I'm not sure at all if this is the same as what the OP had in mind originally, just saying. – aderchox Aug 27 '22 at 11:59