Code Fix

中級 › is possibly 'undefined' の原因と直し方 › パターン5

is possibly 'undefined' の原因と直し方

interfaceで?を付けたオプショナルプロパティは、値が入っていない(undefined)可能性を型として保持します。

 1  interface Settings {
 2    retries?: number;
 3  }
 4  function describe(s: Settings): number {
 5    return s.________ + 1;
                   ^
 6  }
 7  console.log(describe({ retries: 3 }));
main.ts(5,10): error TS18048: 's.retries' is possibly 'undefined'.

空欄を埋めてコンパイルを通す

広告
広告スロット(未設定)

is possibly 'undefined' の原因と直し方

narrowing(存在確認)をせずにメソッドやプロパティへアクセスすると、このエラーになります。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)