自动生成环境模块声明 [英] Automatically generating ambient module declarations

查看:283
本文介绍了自动生成环境模块声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这两个打字稿文件

api/Token.ts

interface Token { 
    code: string
}
export default Token

index.ts

export * from './api/Token'

使用--declarations开关的

tsc 1.5将生成两个.d.ts文件(内容相似)

tsc 1.5 with the --declarations switch will generate two .d.ts files (with similar content)

api/Token.d.ts

interface Token { 
    code: string;
}
export default Token;

index.d.ts

export * from './api/Token';

使用以下选项运行 grunt-dts-bundle

    dts_bundle: {
        release: {
            options: {
                name: 'my-module',
                main: 'index.d.ts'
            }
        }
    }

将生成具有以下内容的环境模块声明文件my-module.d.ts

will generate an ambient module declaration file my-module.d.ts with the following content

declare module 'my-module' {
    export * from './api/Token';
}

但是由于以下原因,该声明无法编译:Import or export declaration in an ambient module declaration cannot reference module through relative module name.

However this declaration does not compile due to : Import or export declaration in an ambient module declaration cannot reference module through relative module name.

我如何自动为上面的两个打字稿文件生成一个环境模块声明?

How can I automatically generate an ambient module declaration for the two typescript files above ?

编辑

请关注 https://github.com/Microsoft/TypeScript/issues/2262上的最新更新.

推荐答案

我最近写了一个 autodts ,其中包含以下内容:

I've recently written a blog post about this. To summarize, you can use autodts if you replace index.ts with api.ts, containing the following:

export {default as Token} from './api/Token';

确保api.tsapi目录位于同一位置(在其旁边,而不是在其中).

Make sure api.ts is in the same place as the api directory (next to, not inside it).

然后您需要一个package.json文件:

{
  "name": "api",
  "version": "1.0.0",
  "main": "dist/api.js",
  "scripts": {
    "preinstall": "npm install autodts",
    "postinstall": "autodts link",
    "prepublish": "tsc && autodts generate"
  },
  "typescript": {
    "definition": "index.d.ts"
  },
  "dependencies": {
    "autodts": "~0.0.4"
  },
  "devDependencies": {
    "@lib/autodts-generator": "~0.0.1",
    "typescript": "~1.5.3"
  }
}

重要的是,程序包名称api必须与文件api.ts和目录api相匹配.这样,使用包时,Node.js和TypeScript编译器将在同一位置查找.

It's important that the package name api matches the file api.ts and directory api. This way both Node.js and the TypeScript compiler will look in the same places when using your package.

最后,您需要一个tsconfig.json文件:

Finally, you need a tsconfig.json file:

{
    "compilerOptions": {
        "declaration": true,
        "module": "CommonJS",
        "target": "es5",
        "outDir": "dist"
    },
    "files": [
        "api.ts"
    ]
}

现在npm install将编译您的软件包,并生成一个捆绑的index.d.ts文件,该文件在package.jsondefinition设置中定义.

Now npm install will compile your package and produce a bundled index.d.ts file as defined in the definition setting in package.json.

要使用您的包裹,您可以执行以下操作:

To use your package, you can do something like:

/// <reference path = "api/index.d.ts" />

import {Token} from 'api';

class foo {
    key: Token;
}

您可以使用autodts link保持reference path的最新状态,为此检查博客文章和/或autodts文档.

You can use autodts link to keep the reference path up to date, check the blog post and/or autodts docs for that.

生成的index.d.ts包含:

/// <reference path="index.ref.d.ts" />
declare module 'api/Token' {
    interface Token {
        code: string;
    }
    export default Token;

}
declare module 'api' {
    export { default as Token } from 'api/Token';

}

index.ref.d.tsapi.js为空白.

这篇关于自动生成环境模块声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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