【发布时间】:2020-04-07 10:02:32
【问题描述】:
我想知道,如果有一些选项,如何从 TSC 命令中排除 node_modules 包。
我正在使用 Typescript v3.5.3。我的 tsconfig.json 是
{
"compilerOptions": {
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es2018", "dom"],
"sourceMap": true,
"allowJs": true,
"jsx": "preserve",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"typeRoots": ["node_modules/@types"]
},
"exclude": [
"build",
"config",
"node_modules"
],
"include": [
"src"
]
}
还有一个像这样的角脚本
import { Component } from "@angular/core";
@Component({
selector: "app-home",
styleUrls: ["./home.component.less"],
templateUrl: "./home.component.html"
})
export class HomeComponent {
title = "angular home";
constructor(){
this.log(123);
this.log('hello world');
}
log(someArg) {
console.log(someArg);
}
}
现在,如果我将运行 TSC,我预计会出错,因为函数日志是隐式的任何类型。在这种情况下一切正常!但是如果我会使用这个代码
import { Node } from "@alfresco/js-api";
import { Component } from "@angular/core";
@Component({
selector: "app-home",
styleUrls: ["./home.component.less"],
templateUrl: "./home.component.html"
})
export class HomeComponent {
title = "angular home";
selectedNode: Node;
constructor(){
this.log(123);
this.log('hello world');
}
log(someArg) {
console.log(someArg);
}
}
我会从 node_modules 的包中得到错误,比如
node_modules/@alfresco/js-api/src/authentication/processAuth.ts:181:9 - 错误 TS2322:类型 'null' 不可分配给类型 'string |未定义'。
181 this.authentications.basicAuth.username = null; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我知道为什么,因为“noImplicitAny”设置为 true,但我想要的是忽略 node_modules 包!因此,在 tsconfig 中的 Angular 排除选项没有任何意义。我认为,构建是包@alfresco/js-api 包含在脚本中并被检查为一个。
有趣的是,如果我在 React.js 中使用类似的代码,一切都如我所料。当我使用带有“noImplicitAny”= true 的命令“TSC”并且 node_modules 包将被忽略时,是否有一些选项或东西?非常感谢!
【问题讨论】:
-
嗯,我昨天遇到了类似的问题。那么将
node_modules/@alfresco添加到compilerOptions中的exclude数组将不起作用? -
不,不起作用,即使我添加了脚本的确切名称“processAuth.ts”也不起作用。
标签: angular typescript tsc