为什么 TypeScript 声明会生成引用?
我有一个使用 TypeScript 编写的项目(lib 项目),并被另一个使用 TypeScript 编写的项目(app 项目)使用。lib 项目编译为单个 js 文件并生成声明文件。一切按预期工作,但生成的声明文件顶部包含此引用—
/// <reference path="typings/index.d.ts" />
在 lib 项目中,有一个 typings 目录与 tsconfig.json 并列,但它只在 lib 项目中需要。它不应作为声明文件中的引用。以下是我的 tsconfig
{
"compilerOptions": {
"module": "commonjs",
"experimentalDecorators": true,
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "proj-lib.js",
"sourceMap": true
},
"include": [
"./js/**/*.ts"
]
}
为什么我的声明文件包含引用?
临时解决方案
使用 gulp-regex-replace 从声明文件中删除引用。
tsResult.dts
.pipe(replace({regex: /\/\/\/\s*<reference[^>]+>\s*/g, replace: ''})) // remove references in declaration file
.pipe(gulp.dest(target));
这不理想。我希望有一个 tsc 的解决方案。
答案#1
查看 TypeScript 编译器选项的文档 :)
https://www.typescriptlang.org/docs/handbook/compiler-options.html
--declaration 布尔值 (默认)false 生成相应的‘.d.ts’文件。
你只需要 typings(.d.ts 文件)的引用
如果你在 tsconfig 中把它关掉(第 3 行的选项),它应该会消失。
干杯