gpt4 book ai didi

angular - 如何在 Angular 的规范测试文件中禁用strictNullChecks?

转载 作者:行者123 更新时间:2023-12-04 04:13:55 26 4
gpt4 key购买 nike

在我的 tsconfig.json 中,我有规则 "strictNullChecks": true 但我不希望在我的测试中包含所有 *.spec.ts 文件。所以我试图在我的 tsconfig.spec.json 中覆盖这个标志,但它不起作用。我究竟做错了什么?

配置文件

{
"compileOnSave": false,
"compilerOptions": {
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"downlevelIteration": true,
"importHelpers": true,
"module": "esnext",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"alwaysStrict": true,
"skipLibCheck": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"baseUrl": "./ClientApp",
},
"angularCompilerOptions": {
"strictTemplates": true,
"strictOutputEventTypes": false
}
}

tsconfig.spec.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": "./",
"target": "es5",
"types": [
"jasmine",
"node"
],
"strictNullChecks": false
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}

最佳答案

VS CodeWebStorm 都寻找最接近打开的文件 tsconfig.json 的配置,用于 IDE TS 支持。他们不知道 Angular 的特定文件,例如 tsconfig.app.jsontsconfig.spec.json
因此,坏消息是似乎不可能仅根据文件扩展名来支持不同的首选项集。分离只能在文件夹级别完成,这意味着您需要将所有测试放在一个文件夹中,并将应用程序代码放在另一个文件夹中。

好消息是,使用 angular-cli 一切都应该正确编译。

作为一种解决方法,可以使用断言

const y: string = null as any;

或者在更改签名后允许跟踪错误的更安全的方法显示错误:
function f(dep: string) () { }
const x = f(null as any); // Ok

// And it won't notify you after changing the type of function argument
// what can be desirable in some cases
function f(dep: number) () { }
const x = f(null as any as number); // Ok and will show error for incorrect type

关于angular - 如何在 Angular 的规范测试文件中禁用strictNullChecks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61119808/

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