Generate declaration file with single module in TypeScript

佐手、 提交于 2021-01-27 04:30:40

问题


Given the following folder structure:

src/
├── foo.ts
├── bar.ts
├── baz.ts
├── index.ts

Where foo.ts, bar.ts, and baz.ts each export a default class or thing: i.e. in the case of foo.ts:

export default class Foo {
    x = 2;
}

Can we automatically generate a declaration file which declares one module my-module and exports foo.ts, bar.ts, and baz.ts as non-defaults?

I.e. I want tsc to generate the following:

build/
├── foo.js
├── bar.js
├── baz.js
├── index.js
├── index.d.ts

Where index.d.ts contains:

declare module 'my-module' {
    export class Foo {
        ...
    }
    export class Bar {
        ...
    }
    export class Baz {
        ...
    }
}

I see that mostly all NPM modules have a declaration file like this and maybe with separate files.

How would I accomplish this?


回答1:


In your tsconfig.json file, set "declaration" to true. Then when you run typescript on your project it will automatically generate declaration files for you.



来源:https://stackoverflow.com/questions/53059922/generate-declaration-file-with-single-module-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!