在打字稿 2.6 中,我想编写一个执行空检查的函数。当我启用严格的空检查时,打字稿 2.6 会抱怨以下代码。(注意,当使用空检查时直接有效)
编辑:更正,notNullOrUndefined因为它没有检查 foo
interface A {
foo: string | undefined;
}
const notNullOrUndefined = (a: A): boolean => {
return a.foo != null;
}
const len = (a: A): number => {
//if (a.foo != null) {
if (notNullOrUndefined(a)){
return a.foo.length;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是要玩的示例:示例
解决这个问题的打字稿方法是什么?