strictNullChecks
strictNullChecksはnullやundefinedのチェックを厳しくするコンパイラオプションです。
- デフォルト: strictが有効の場合は
true、それ以外はfalse - 追加されたバージョン: 2.0
- TypeScript公式が有効化推奨
nullとundefinedが代入できる危険性
TypeScriptではstrictNullChecksがfalseの場合、nullとundefinedの代入がチェックされません。非null型や非undefined型の変数にも、nullとundefinedが代入できます。
strictNullChecksがfalseの場合tsconstdate :Date = null; // OKconsterror :Error =undefined ; // OK
strictNullChecksがfalseの場合tsconstdate :Date = null; // OKconsterror :Error =undefined ; // OK
nullやundefinedにはプロパティが存在しません。そのため、JavaScript実行時にエラーになります。
tsdate .getDay ();
tsdate .getDay ();
strictNullChecksがtrueの場合、非null型へのnullの代入、非undefined型へのundefinedの代入それぞれがコンパイルエラーになります。
strictNullChecksがtrueの場合tsconstType 'null' is not assignable to type 'Date'.2322Type 'null' is not assignable to type 'Date'.: date Date = null;constType 'undefined' is not assignable to type 'Error'.2322Type 'undefined' is not assignable to type 'Error'.: error Error =undefined ;
strictNullChecksがtrueの場合tsconstType 'null' is not assignable to type 'Date'.2322Type 'null' is not assignable to type 'Date'.: date Date = null;constType 'undefined' is not assignable to type 'Error'.2322Type 'undefined' is not assignable to type 'Error'.: error Error =undefined ;
関数の戻り値の型への影響
strictNullChecksの設定によって、関数の戻り値の型が変わることがあります。配列のfindメソッドの戻り値の型は、要素の型もしくはundefinedです。しかし、strictNullChecksがfalseの場合、戻り値がundefinedになる可能性をコンパイラが考えなくなります。戻り値にnullが入る可能性がある関数、たとえばgetElementByIdの場合も同様です。
strictNullChecksがfalseの場合tsconstresult = [1, 2, 3].find ((x ) =>x == 1);constelement =document .getElementById ("main");
strictNullChecksがfalseの場合tsconstresult = [1, 2, 3].find ((x ) =>x == 1);constelement =document .getElementById ("main");
strictNullChecksがtrueの場合は、undefinedやnullが戻り値になる可能性をコンパイラが考慮します。そのため、findなら要素の型とundefinedのユニオン型に、getElementByIdならHTMLElement | nullになります。
strictNullChecksがtrueの場合tsconstresult = [1, 2, 3].find ((x ) =>x == 1);constelement =document .getElementById ("main");
strictNullChecksがtrueの場合tsconstresult = [1, 2, 3].find ((x ) =>x == 1);constelement =document .getElementById ("main");
この設定の効果は、ユーザー定義の型ガード関数にも及びます。たとえば、関数の戻り値をstring | undefinedと型注釈したとしても、strictNullChecksがfalseの場合はstring型になります。
strictNullChecksがfalseの場合ts// ユーザー定義の型ガード関数functiongetStringOrUndefined (): string | undefined {returnundefined ;}constvalue =getStringOrUndefined ();
strictNullChecksがfalseの場合ts// ユーザー定義の型ガード関数functiongetStringOrUndefined (): string | undefined {returnundefined ;}constvalue =getStringOrUndefined ();
strictNullChecksは有効にしよう
nullやundefinedを期待しない変数にそれらが代入できるのは危険です。また、関数の戻り値にnullやundefinedが入る可能性が見えなくなることも、思わぬバグを生む原因になります。strictNullChecksはtrueを設定するのがお勧めです。
学びをシェアする
😱TypeScriptデフォルトでnullとundefinedの代入チェックをしない(どんな型にも代入できる)
✅コンパイラオプションstrictNullChecksをtrueにすると、nullとundefinedの代入がチェックされる
👍strictNullChecksは有効にしよう
『サバイバルTypeScript』より
関連情報
📄️ strict
strict系のオプションを一括で有効化する
📄️ null型
JavaScriptのnullは値がないことを示す値です。
📄️ undefined型
JavaScriptのundefinedは未定義を表すプリミティブな値です。変数に値がセットされていないとき、戻り値が無い関数、オブジェクトに存在しないプロパティにアクセスしたとき、配列に存在しないインデックスでアクセスしたときなどに現れます。