Module 自动生成环境模块声明
给定这2个类型脚本文件Module 自动生成环境模块声明,module,scripting,typescript,Module,Scripting,Typescript,给定这2个类型脚本文件 api/Token.ts 和index.ts 带有--declarations开关的tsc 1.5将生成两个.d.ts文件(具有类似的内容) api/Token.d.ts 和index.d.ts 使用以下选项运行grunt dts捆绑包 dts_bundle: { release: { options: { name: 'my-module', main: 'i
api/Token.ts
和index.ts
带有--declarations
开关的tsc 1.5将生成两个.d.ts
文件(具有类似的内容)
api/Token.d.ts
和index.d.ts
使用以下选项运行grunt dts捆绑包
dts_bundle: {
release: {
options: {
name: 'my-module',
main: 'index.d.ts'
}
}
}
将生成包含以下内容的环境模块声明文件my module.d.ts
declare module 'my-module' {
export * from './api/Token';
}
但是,由于:环境模块声明中的导入或导出声明不能通过相对模块名称引用模块,因此无法编译此声明。
如何为上述两个typescript文件自动生成环境模块声明
编辑
请关注上的最新更新。我最近写了一篇关于这方面的文章。总之,如果将index.ts
替换为api.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"
}
}
{
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"target": "es5",
"outDir": "dist"
},
"files": [
"api.ts"
]
}
包名api
必须与文件api.ts
和目录api
匹配。这样,Node.js和TypeScript编译器在使用包时将在相同的位置查找
最后,您需要一个tsconfig.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"
}
}
{
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"target": "es5",
"outDir": "dist"
},
"files": [
"api.ts"
]
}
现在,npm install
将编译您的包并生成一个捆绑的index.d.ts
文件,如package.json中的定义
设置中所定义的
要使用软件包,您可以执行以下操作:
/// <reference path = "api/index.d.ts" />
import {Token} from 'api';
class foo {
key: Token;
}
index.ref.d.ts
和api.js
为空。感谢您的详细回答,并对迟来的回复表示歉意,我正在等待测试TypeScript 1.6,它完全改变了交易。您的解决方案是TypeScript<1.6。对于TypeScript1.6,GitHub问题中有一些细节:@BrunoGrieder确实不清楚。我已经阅读了一页又一页关于github问题的评论,但是找不到这个非常简单问题的答案。例如,在typescript>1.6的情况下,您是否希望编写一个使用“inversify”模块(已键入)的库,然后还希望在库外部使用inversify。你是如何做到的???@David我从来没有使用过inversify,但是你可以找到一个样板库和我写的一些细节,在底部有点类似
{
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"target": "es5",
"outDir": "dist"
},
"files": [
"api.ts"
]
}
/// <reference path = "api/index.d.ts" />
import {Token} from 'api';
class foo {
key: Token;
}
/// <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';
}