[英]Typescript throws no error when calling nullable function
我的反应应用程序中有以下代码:
如您所见onCheck
可以是undefined
。 但 typescript 没有显示错误,我收到运行时错误。
我必须添加这个道具没有默认值。
我的 tsconfig:
{
"compileOnSave": false,
"compilerOptions": {
"sourceMap": true,
"target": "es5",
"module": "esnext",
"declaration": false,
"noImplicitAny": false,
"experimentalDecorators": true,
"noLib": false,
"jsx": "react",
"noEmitOnError": true,
"moduleResolution": "node",
"lib": [
"DOM",
"ES5",
"ScriptHost",
"ES2015",
"ES2018.Promise"
],
"allowSyntheticDefaultImports": true
},
"exclude": [
"node_modules"
]
}
在compilerOptions
下,在tsconfig.json
中添加"strict": true
。
从文档:
严格标志启用了范围广泛的类型检查行为,从而更有效地保证了程序的正确性。 打开它相当于启用所有严格模式系列选项,如下所述。
alwaysStrict
strictNullChecks
strictBindCallApply
strictFunctionTypes
strictPropertyInitialization
noImplicitAny
noImplicitThis
如果您只关心可空变量,那么只需使用"strictNullChecks"
标志。
您没有在tsconfig.json中设置相应的编译器选项
要么添加
"strictNullChecks": true
仅启用null
检查,或(推荐)添加
"strict": true
添加范围广泛的检查。
请注意,当您升级 typescript 版本时,使用"strict": true
可能会破坏您的构建。 因为新的strict...
标志将自动使用该标志打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.