中級 › is possibly 'undefined' の原因と直し方 › パターン3
is possibly 'undefined' の原因と直し方
interfaceで?を付けたオプショナルプロパティは、値が入っていない(undefined)可能性を型として保持します。
1 interface Options { 2 timeout?: number; 3 } 4 function describe(o: Options): number { 5 return o.________ + 1; ^ 6 } 7 console.log(describe({ timeout: 9 }));
main.ts(5,10): error TS18048: 'o.timeout' is possibly 'undefined'.
空欄を埋めてコンパイルを通す
timeoutはオプショナルなnumberです。undefinedかもしれない値に対して+演算はできません。
次の問題