noUncheckedIndexedAccess
from TypeScript 4.1
Checked Indexed Accesses (--noUncheckedIndexedAccess)
index signaturesという機能がある
code:ts
const a: Record<string, number> = {
a: 3,
b: 4
}
a.c // number | undefinedであるべき
// noUncheckedIndexedAccessがtrueだとそうなる。falseだとnumberが推論されてしまう
普通にいつもtrueにしておくべき設定
typoに気づけなかったりする
https://zenn.dev/lollipop_onl/articles/eoz-ts-no-unchecked-indexed-access
こういうextra propertiesが異なる型のときとかも同様
code:ts
interface Options {
path: string;
permissions: number;
// Extra properties are caught by this index signature.
propName: string: string | number;
}
function checkOptions(opts: Options) {
opts.path; // string
opts.permissions; // number
// These are all allowed too!
// They have the type 'string | number'.
opts.yadda.toString();
opts"foo bar baz".toString();
optsMath.random().toString();
}
opts.permisssssions.toString()