在TypeScript中使用单个模块生成声明文件 [英] Generate declaration file with single module in TypeScript

查看:114
本文介绍了在TypeScript中使用单个模块生成声明文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下文件夹结构:

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

其中 foo.tsbar.tsbaz.ts 分别导出一个默认的类或事物:即foot.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;
}

我们可以自动生成一个声明文件,声明一个模块 my-module 并导出foo.tsbar.tsbaz.ts 作为非默认值?

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?

即我希望 tsc 生成以下内容:

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

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

其中 index.d.ts 包含:

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

我看到大多数 NPM 模块都有这样的声明文件,可能还有单独的文件.

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

我将如何做到这一点?

推荐答案

在 tsconfig.json 文件中,将declaration"设置为 true.然后当你在你的项目上运行 typescript 时,它会自动为你生成声明文件.

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.

这篇关于在TypeScript中使用单个模块生成声明文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆