自动生成环境模块声明
Automatically generating ambient module declarations
给定这 2 个打字稿文件
api/Token.ts
interface Token {
code: string
}
export default Token
和index.ts
export * from './api/Token'
带有 --declarations
开关的 tsc 1.5 将生成两个 .d.ts
文件(内容相似)
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
,内容如下
declare module 'my-module' {
export * from './api/Token';
}
但是由于以下原因,此声明无法编译:Import or export declaration in an ambient module declaration cannot reference module through relative module name.
如何自动为上面的两个打字稿文件生成环境模块声明?
编辑
上的最新更新
我最近写了一个 blog post about this. To summarize, you can use autodts 如果你用 api.ts
替换 index.ts
,包含以下内容:
export {default as Token} from './api/Token';
确保 api.ts
与 api
目录位于同一位置(在其旁边,而不是在其内部)。
那么你需要一个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 编译器在使用你的包时会在相同的地方查找。
最后,你需要一个tsconfig.json
文件:
{
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"target": "es5",
"outDir": "dist"
},
"files": [
"api.ts"
]
}
现在 npm install
将编译您的包并生成一个捆绑的 index.d.ts
文件,如 package.json
中的 definition
设置所定义。
要使用您的包,您可以执行以下操作:
/// <reference path = "api/index.d.ts" />
import {Token} from 'api';
class foo {
key: Token;
}
您可以使用 autodts link
使 reference path
保持最新,查看博客 post and/or autodts 文档。
结果 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.ts
和 api.js
为空白。
给定这 2 个打字稿文件
api/Token.ts
interface Token {
code: string
}
export default Token
和index.ts
export * from './api/Token'
带有 --declarations
开关的 tsc 1.5 将生成两个 .d.ts
文件(内容相似)
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
,内容如下
declare module 'my-module' {
export * from './api/Token';
}
但是由于以下原因,此声明无法编译:Import or export declaration in an ambient module declaration cannot reference module through relative module name.
如何自动为上面的两个打字稿文件生成环境模块声明?
编辑
上的最新更新我最近写了一个 blog post about this. To summarize, you can use autodts 如果你用 api.ts
替换 index.ts
,包含以下内容:
export {default as Token} from './api/Token';
确保 api.ts
与 api
目录位于同一位置(在其旁边,而不是在其内部)。
那么你需要一个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 编译器在使用你的包时会在相同的地方查找。
最后,你需要一个tsconfig.json
文件:
{
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"target": "es5",
"outDir": "dist"
},
"files": [
"api.ts"
]
}
现在 npm install
将编译您的包并生成一个捆绑的 index.d.ts
文件,如 package.json
中的 definition
设置所定义。
要使用您的包,您可以执行以下操作:
/// <reference path = "api/index.d.ts" />
import {Token} from 'api';
class foo {
key: Token;
}
您可以使用 autodts link
使 reference path
保持最新,查看博客 post and/or autodts 文档。
结果 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.ts
和 api.js
为空白。