How to create TypeScript definition file (d.ts) for umd library
Asked Answered
S

2

13

I am working on the library surveyjs

It uses gulp+webpack to build umd bundle.

I want to create the type definition bundle (or may be just multiple d.ts files) for using in typescript projects. I would like to have something like that:

import * as Survey from 'surveyjs';

All contens for Survey.* is described here: https://github.com/dmitrykurmanov/surveyjs/blob/master/src/entries/ko.ts

I have tried to use: github.com/SitePen/dts-generator and github.com/TypeStrong/dts-bundle but whithout success, could somebody please show me the right direction?

Skilken answered 20/10, 2016 at 12:55 Comment(2)
uhm.. you can configure tsc in tscconfig.json to output declarations as well using the declaration flag.Phallic
Finally we've implemented it with dts-bundleSkilken
P
6

You can ask tsc to generate the declaration files for your code by adding the declaration flag in tsconfig.json.

In your case it would be:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "sourceMap": true,
    "noImplicitAny": false,
    "jsx": "react",
    "declaration": true
  },
//  "filesGlob": [
  //    "typings/index.d.ts"
  //  ], // TODO
  "include": [
    "typings/index.d.ts",
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}
Phallic answered 20/10, 2016 at 13:41 Comment(4)
Yes, it is. The problem in webpack-stream output. It is just js bundle, but how can I get d.ts? So I run 'gulp-typescript' compiler separately and generate d.ts. It is not cool.Skilken
that really depends a lot on your setup.. if you want a more specific answer you should share more of that.Phallic
And another problem is bundle.d.ts. Is it possible to create it? I found two projects github.com/TypeStrong/dts-bundle and github.com/SitePen/dts-generator I failed with config them. Have someone any success examples?Skilken
@tokv, ok I will create another questions about webpack-stream and bundle.d.tsSkilken
O
2

As toskv mentioned you can use tsc to generate the .d.ts files. Problem there is that the compiler creates multiple declaration files, one for each of your .ts file. I had the same problem with another projected I worked on. I soved it by creating a small plugin for webpack which merges the .d.ts files generated by tsc.

You can check it out on here: https://www.npmjs.com/package/typescript-declaration-webpack-plugin

Obsequious answered 20/7, 2018 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.