noUncheckedIndexedAccess
リリースされたバージョン: 4.1
インデックス型や配列で宣言されたオブジェクトが持つプロパティへのアクセスを厳密に評価します。
typescripttype ObjectLiteralLike = {en: string;fr: string;it: string;[lang: string]: string;};type ArrayObjectLike = {0: string;1: string;[num: number]: string;};function log(s: string): void {console.log(s);}const butterfly: ObjectLiteralLike = {en: "Butterfly",fr: "Papillon",it: "Farfalla",es: "Mariposa",};const phoneticCodes: ArrayObjectLike = {0: "alpha",1: "bravo",2: "charlie",};log(spanish);log(third);
typescripttype ObjectLiteralLike = {en: string;fr: string;it: string;[lang: string]: string;};type ArrayObjectLike = {0: string;1: string;[num: number]: string;};function log(s: string): void {console.log(s);}const butterfly: ObjectLiteralLike = {en: "Butterfly",fr: "Papillon",it: "Farfalla",es: "Mariposa",};const phoneticCodes: ArrayObjectLike = {0: "alpha",1: "bravo",2: "charlie",};log(spanish);log(third);
ObjectLiteralLike, ArrrayObjectLikeは共にstring型のプロパティを持つオブジェクトの型として宣言されています。
typescriptconst spanish: string = butterfly.es;const third: string = phoneticCodes[2];
typescriptconst spanish: string = butterfly.es;const third: string = phoneticCodes[2];
これらのオブジェクトのプロパティにアクセスするときは完全な型安全ではありません。このオプションを有効にすると次のようなエラーが発生します。
texterror TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. log(spanish); ~~~~~~~ error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. log(third); ~~~~~
texterror TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. log(spanish); ~~~~~~~ error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. log(third); ~~~~~
このように厳密に定義されていないプロパティはundefined型とのユニオン型として解釈されるようになります。
typescriptconst spanish: string | undefined = butterfly.es;const third: string | undefined = phoneticCodes[2];
typescriptconst spanish: string | undefined = butterfly.es;const third: string | undefined = phoneticCodes[2];
配列はインデックス記法でアクセスをするとundefined型とのユニオン型と解釈されますがfor-of, array.forEach()はこの制約を受けないため積極的に使用を検討してください。
typescriptconst phoneticCodes: string[] = ["alpha", "bravo", "charlie"];for (const p of phoneticCodes) {// ...}phoneticCodes.forEach((p: string) => {// ...});
typescriptconst phoneticCodes: string[] = ["alpha", "bravo", "charlie"];for (const p of phoneticCodes) {// ...}phoneticCodes.forEach((p: string) => {// ...});