TypescriptプロジェクトでstrictNullChecks
フラグを使用しています。Typescript strictNullChecks構文
function hello(a: string | null) {
if (a !== null) {
console.log(a.length); // Here, "a" can only be a string
}
}
コンパイラは完全にここに動作します:
は、以下の機能を考えてみましょう。
は、しかし、私が代わりにif (typeof a !== "null")
あるいは書く場合lodashのif (!_.isNull(a))
コンパイラが理解できないだろうとa
はおそらくnull
可能と文句を言うだろう。
これらの代替構文を使用する方法はありますか?
本当にこのコンパイラフラグが何をしていますか? https://www.typescriptlang.org/docs/handbook/compiler-options.html – Fals
また、 'typeof(a = null)==="オブジェクト "' [_forforforeacy reasons_](https://developer.mozilla。 –
ロダッシュの型が更新され、 'if(!_ isNull(a))'が適切な型のガードを今すぐ行う必要があります –