【问题标题】:Why are Typescript Declarations Generated With References?为什么使用引用生成 Typescript 声明?
【发布时间】:2016-09-06 13:29:06
【问题描述】:

我有一个用 typescript 编写的项目(lib 项目),并被另一个用 typescript 编写的项目(app 项目)使用。 lib 项目编译为单个 js 文件并生成声明文件。一切都按预期工作,但生成的声明文件在顶部包含此引用-

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

在 lib 项目中,tsconfig.json 旁边有一个 typings 目录,但它只是 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 解决方案。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    检查文档以获取 typescript 编译器选项 :)

    https://www.typescriptlang.org/docs/handbook/compiler-options.html

    --declaration boolean (default)false 生成对应的‘.d.ts’文件。

    您只需要打字参考(.d.ts 文件)

    如果您在 tsconfig(选项第 3 行)中关闭 if,它应该会消失。

    干杯

    【讨论】:

    • 我不明白你的回答。我想要d.ts 文件,但我不想在这些生成的文件中引用。
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2013-08-20
    • 2015-12-08
    • 2021-12-31
    • 2022-06-20
    • 1970-01-01
    • 2019-04-03
    • 2016-03-26
    相关资源
    最近更新 更多