TypeScript 编译器在我的项目中没有按预期抛出错误
TypeScript compiler not throwing error when expected in my project
我的一个项目遇到了一些麻烦,TypeScript 编译器没有在我期望的时候抱怨类型不匹配。当我在 TS playground 中尝试相同的代码块时,它确实会抱怨,正如我所预料的那样。不确定我的项目配置是否有问题 - 我没有注意到我的项目中有任何其他类似问题。
这是有问题的示例代码:
const func = (opt: { a: number; b: number; c: number }) => undefined;
const func2 = (arg: (opt: { a: number; b: number }) => undefined) => undefined;
func2(func);
这与在 playground 中导致错误的代码相同:
这是我的 tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"allowJs": false,
"moduleResolution": "node",
"lib": [ "es2015" ],
"experimentalDecorators": true,
"useDefineForClassFields": true,
"downlevelIteration": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"outDir": "./dist/",
"baseUrl": "./src",
"paths": {
"@lib/*": ["../../lib/src/*"], // relative to baseUrl
},
"lib": [ "dom" ],
"jsx": "react",
},
"types": [
"babylonjs"
],
"typeRoots": [
"./node_modules/@types"
],
"plugins": [
{
"name": "typescript-plugin-css-modules"
}
]
}
我在我的项目中使用的是 TypeScript 版本 4.3.5
。
您不需要激活 strictFunctionTypes 标志。
When enabled, this flag causes functions parameters to be checked more correctly.
此外,值得使用 strict: true。
The strict flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness
我的一个项目遇到了一些麻烦,TypeScript 编译器没有在我期望的时候抱怨类型不匹配。当我在 TS playground 中尝试相同的代码块时,它确实会抱怨,正如我所预料的那样。不确定我的项目配置是否有问题 - 我没有注意到我的项目中有任何其他类似问题。
这是有问题的示例代码:
const func = (opt: { a: number; b: number; c: number }) => undefined;
const func2 = (arg: (opt: { a: number; b: number }) => undefined) => undefined;
func2(func);
这与在 playground 中导致错误的代码相同:
这是我的 tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"allowJs": false,
"moduleResolution": "node",
"lib": [ "es2015" ],
"experimentalDecorators": true,
"useDefineForClassFields": true,
"downlevelIteration": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"outDir": "./dist/",
"baseUrl": "./src",
"paths": {
"@lib/*": ["../../lib/src/*"], // relative to baseUrl
},
"lib": [ "dom" ],
"jsx": "react",
},
"types": [
"babylonjs"
],
"typeRoots": [
"./node_modules/@types"
],
"plugins": [
{
"name": "typescript-plugin-css-modules"
}
]
}
我在我的项目中使用的是 TypeScript 版本 4.3.5
。
您不需要激活 strictFunctionTypes 标志。
When enabled, this flag causes functions parameters to be checked more correctly.
此外,值得使用 strict: true。
The strict flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness