noUncheckedIndexedAccess
Checked Indexed Accesses (--noUncheckedIndexedAccess)
code:ts
const a: Record<string, number> = {
a: 3,
b: 4
}
a.c // number | undefinedであるべき
// noUncheckedIndexedAccessがtrueだとそうなる。falseだとnumberが推論されてしまう
普通にいつもtrueにしておくべき設定
typoに気づけなかったりする
こういうextra propertiesが異なる型のときとかも同様
code:ts
interface Options {
path: string;
permissions: number;
// Extra properties are caught by this index signature.
}
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.permisssssions.toString()