TypeScript 不检查可选参数的兼容性
TypeScript not checking for optional arguments compatibility
尽管我的 tsconfig.json
中有 strict: true
,但没有错误:
const a: (b?: string) => string[] =
(c : string) => c.split(',');
^
should scream in vein
如何让 TypeScript 对此感到恐慌?
包版本:
- 打字稿 2.5.3
- ts-loader 3.1.1
- 网络包 3.6.0
这是我的完整 tsconfig.json
:
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true,
"noImplicitAny": true,
"allowJs": true,
"sourceMap": true,
"allowSyntheticDefaultImports": false,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": false,
"preserveConstEnums": false,
"removeComments": false,
"lib": [
"es5",
"es6",
"dom",
"es2015.core",
"es2015.collection",
"es2015.generator",
"es2015.iterable",
"es2015.promise",
"es2015.proxy",
"es2015.reflect",
"es2015.symbol",
"es2015.symbol.wellknown",
"esnext.asynciterable"
]
},
"exclude": [
"node_modules",
"test",
".git"
]
}
您的设置中发生了一些奇怪的事情。我将 TypeScript 2.6.1 与您的 tsconfig.json
和代码一起使用:
const a: (b?: string) => any = (b: string) => 1;
因为您打开了 strict
标志,所以它包含了您需要得到错误的两个标志; strictNullChecks
和 strictFunctionTypes
。当我 运行:
tsc
我收到消息:
app.ts(1,7): error TS2322: Type '(b: string) => number' is not assignable to type '(b?: string | undefined) => any'. Types of parameters 'b' and 'b' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
您运行如何编译编译器?我问的原因是,如果你 "run it plain" ,它只会使用你的配置文件,如上所示。例如,如果您传递一个文件名参数,您将不会使用您的配置:
tsc app.ts
结果没有错误,因为您不再使用 tsconfig.json
。
尽管我的 tsconfig.json
中有 strict: true
,但没有错误:
const a: (b?: string) => string[] =
(c : string) => c.split(',');
^
should scream in vein
如何让 TypeScript 对此感到恐慌?
包版本:
- 打字稿 2.5.3
- ts-loader 3.1.1
- 网络包 3.6.0
这是我的完整 tsconfig.json
:
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true,
"noImplicitAny": true,
"allowJs": true,
"sourceMap": true,
"allowSyntheticDefaultImports": false,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": false,
"preserveConstEnums": false,
"removeComments": false,
"lib": [
"es5",
"es6",
"dom",
"es2015.core",
"es2015.collection",
"es2015.generator",
"es2015.iterable",
"es2015.promise",
"es2015.proxy",
"es2015.reflect",
"es2015.symbol",
"es2015.symbol.wellknown",
"esnext.asynciterable"
]
},
"exclude": [
"node_modules",
"test",
".git"
]
}
您的设置中发生了一些奇怪的事情。我将 TypeScript 2.6.1 与您的 tsconfig.json
和代码一起使用:
const a: (b?: string) => any = (b: string) => 1;
因为您打开了 strict
标志,所以它包含了您需要得到错误的两个标志; strictNullChecks
和 strictFunctionTypes
。当我 运行:
tsc
我收到消息:
app.ts(1,7): error TS2322: Type '(b: string) => number' is not assignable to type '(b?: string | undefined) => any'. Types of parameters 'b' and 'b' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
您运行如何编译编译器?我问的原因是,如果你 "run it plain" ,它只会使用你的配置文件,如上所示。例如,如果您传递一个文件名参数,您将不会使用您的配置:
tsc app.ts
结果没有错误,因为您不再使用 tsconfig.json
。