gpt4 book ai didi

typescript - 为什么需要 exclude= ["node_modules"] 而我的 tsconfig.json 中只有 include= ["src"]?

转载 作者:行者123 更新时间:2023-12-05 03:53:53 25 4
gpt4 key购买 nike

我只想编译“src”文件夹。因此我做 include:["src"],并且 node_modules 只在 src 之外找到,那么为什么我需要排除它们,比如 exclude: ["node_modules"]正如许多网站所建议的那样?

例如他们还建议我们包括 src 然后也排除 node_modules - https://www.javatpoint.com/typescript-compilation-context

{  
"compilerOptions": {
"module": "system",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "../../built/local/tsc.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}

我不应该在我的 tsconfig.json 中写任何排除项,对吧?

最佳答案

你理解错了。 include 中的值array 代表将要包含在程序中的文件夹/文件。 exclude然后指定哪些文件夹/文件必须从包含的文件夹/文件中排除。

换句话说,如果你写include: ['src']然后 exclude: ['*.spec.ts'] , 然后你排除每一个 *.spec.ts src 下找到的文件文件夹。

您必须排除 node_modules ,即使该文件夹位于 src 之外文件夹。那是因为这些包是导入(使用 import )在放置在包含的 src 中的文件(也称为模块)中。文件夹。如果您不这样做,TypeScript 也会检查导入的包,这不是您想要的。

因为建议总是排除node_modules , TypeScript 决定将其添加为默认值。因此,您可以省略整个 exclude如果您只想排除 node_modules 的规范.

因为您还想排除 *.spec.ts文件(不是默认的),你需要按照你做的方式指定它。

还有更多文件夹作为默认值添加到数组中["node_modules", "bower_components", "jspm_packages"]

您可以阅读有关此主题的更多信息 in the docs .

关于typescript - 为什么需要 exclude= ["node_modules"] 而我的 tsconfig.json 中只有 include= ["src"]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61451948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com