5

How to generate “*.d.ts” in typescript or creates and import @types? Is there any way to create d.ts for jquery plugin or plain javascript library?

Mantu Nigam
  • 3,690
  • 7
  • 24
  • 41
  • 1
    if it's your own code witten in typescript you can enable the generation as a compiler option. if it's a thirdparty not written in typescript you have to write them manually. – toskv Jul 14 '17 at 11:25
  • If no one else have created a definition for `@types/your-jquery-thingie` you can do so yourself, see https://github.com/DefinitelyTyped/DefinitelyTyped/#how-can-i-contribute – danneth Jul 14 '17 at 12:04
  • 1
    Possible duplicate of [How do you produce a .d.ts "typings" definition file from an existing JavaScript library?](https://stackoverflow.com/questions/12687779/how-do-you-produce-a-d-ts-typings-definition-file-from-an-existing-javascript) – Juliën Jul 14 '17 at 12:29

3 Answers3

10

If you want to create the declaration file only, without producing other .js files, you can run the compiler with the emitDeclarationOnly option

tsc --emitDeclarationOnly

Here is a full list of available compiler options.

Jordan Bonitatis
  • 1,527
  • 14
  • 12
9

File .d.ts is a declaration file.For details see.

To create .d.ts from a ts file.You only need to add the -d or --declaration parameter during compile using tsc.

Example:

tsc -d your_ts_file.ts
Laode Muhammad Al Fatih
  • 3,994
  • 1
  • 18
  • 32
1

For your own code, this can be done with a compiler option:

tsc --sourceMap=true

This can also be added to your tsconfig file, as well as MSBuild task.

For jQuery, most of those libraries are on DefinitelyTyped, where you can install them via @types/LIBRARY_NAME.

For other third party libraries, you may have to create them yourself; otherwise, you can allow JavaScript with another compiler option:

tsc --allowJs=true

Then depending on your loading tool, you can use require or some other mechanism to import.