TypeScript 2.0 从排除的文件中抛出错误? [英] TypeScript 2.0 throws errors from excluded files?

查看:37
本文介绍了TypeScript 2.0 从排除的文件中抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 this 种子项目的 nodeJS 项目.它有两个 tsconfig.json 文件,如下所示:

I have a nodeJS project based on this seed project. It has two tsconfig.json files which look like this:

{
    "compilerOptions": {
        "target": "es6",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    },
    "exclude": [
        "../node_modules"
    ]
}

然而,尽管排除了 node_modules,我还是收到了很多错误,其中一些我已经在下面显示了.自从我开始使用新的 npm i @types/xyz 方法以来,就发生了这种情况.

however, despite the exclude node_modules, I get loads of errors, some of which I've shown below. This has occured since I started using the new npm i @types/xyz approach.

[0] node_modules/@types/core-js/index.d.ts(21,14): 错误 TS2300: 重复标识符PropertyKey".

[0] node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate identifier 'PropertyKey'.

[0] node_modules/@types/core-js/index.d.ts(85,5):错误 TS2687:'name' 的所有声明必须具有相同的修饰符.

[0] node_modules/@types/core-js/index.d.ts(85,5): error TS2687: All declarations of 'name' must have identical modifiers.

[0] node_modules/@types/core-js/index.d.ts(145,5): 错误 TS2403: 后续变量声明必须具有相同的类型.变量 '[Symbol.unscopables]' 必须是类型 '{ copyWithin: boolean;条目:布尔值;填充:布尔值;查找:布尔值;查找索引:布尔值;键:...',但这里输入的是'any'.

[0] node_modules/@types/core-js/index.d.ts(145,5): error TS2403: Subsequent variable declarations must have the same type. Variable '[Symbol.unscopables]' must be of type '{ copyWithin: boolean; entries: boolean; fill: boolean; find: boolean; findIndex: boolean; keys: ...', but here has type 'any'.

[0] node_modules/@types/core-js/index.d.ts(262,5):错误 TS2687:'flags' 的所有声明必须具有相同的修饰符.

[0] node_modules/@types/core-js/index.d.ts(262,5): error TS2687: All declarations of 'flags' must have identical modifiers.

[0] node_modules/@types/core-js/index.d.ts(276,5):错误 TS2687:'EPSILON' 的所有声明必须具有相同的修饰符.

[0] node_modules/@types/core-js/index.d.ts(276,5): error TS2687: All declarations of 'EPSILON' must have identical modifiers.

[0] node_modules/@types/core-js/index.d.ts(311,5):错误 TS2687:'MAX_SAFE_INTEGER' 的所有声明必须具有相同的修饰符.

[0] node_modules/@types/core-js/index.d.ts(311,5): error TS2687: All declarations of 'MAX_SAFE_INTEGER' must have identical modifiers.

推荐答案

我刚刚使用 anguler2 项目升级到 ts2.0 后遇到了这个问题.我现在使用 typings 并且有很多依赖项.其中至少包括@types,它复制了一些导入.但它们是必需的,我不能只删除文件夹/依赖项(就像其他人建议的那样).

I just ran into this issue after upgrading to ts2.0 with an anguler2 project. I'm using typings for now and have a lot of dependencies. At least on of them is including @types, which duplicates some of the imports. But they are required, I can't just delete the folder / dependency (like other have suggested).

在这里,我找到了一个对我有帮助的解决方案的指针:https://github.com/Microsoft/TypeScript/issues/11257

On here I found a pointer to the solution which helped me: https://github.com/Microsoft/TypeScript/issues/11257

来自 lucassp 的示例:

Sample from lucassp:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "types": []
    },
    "exclude": [
        "../node_modules"
    ]
}

我只需将 "types": [] 添加到 tsconfig.json 文件中,这使得它不包含 @types 中的任何类型(因为它们目前都由类型管理).

I simply had to add "types": [] to the tsconfig.json file, which makes it include none of the types in @types (since all of them are managed by typings currently).

以下是编译器选项的说明来自文档:

Here's the description of the compiler option from the doc:

--types string[]
List of names of type definitions to include. See @types, –typeRoots and –types for more details.

微软似乎认为@types 是新的黑色打字稿定义导入.在这里阅读更多信息.

Microsoft seems to think that @types is the new black typescript definition imports. Read more info on this here.

最终我可能会迁移,但我会先让尘埃落定.

Eventually I might migrate, but I'll let the dust settle, first.

这篇关于TypeScript 2.0 从排除的文件中抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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