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?

Dmitry Kurmanov
  • 765
  • 2
  • 12
  • 19

2 Answers2

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"
  ]
}
toskv
  • 30,680
  • 7
  • 72
  • 74
  • 3
    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. – Dmitry Kurmanov Oct 21 '16 at 14:13
  • that really depends a lot on your setup.. if you want a more specific answer you should share more of that. – toskv Oct 21 '16 at 14:16
  • 1
    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? – Dmitry Kurmanov Oct 21 '16 at 14:16
  • @tokv, ok I will create another questions about webpack-stream and bundle.d.ts – Dmitry Kurmanov Oct 21 '16 at 14:18
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