Let's say I have a type Fields
export type NodeId =
| "a-index"
| "b-index"
| "c-index"
export type Fields = {
[id in NodeId]?: {
[sectionId: string]: {
[name: string]: string;
};
}
};
Why does the first set error with Object is possibly 'undefined', but not the second set?
const test: Fields = {};
const id = "a-index";
// Why does this error
let resultA;
if (test[id]) {
resultA = test[id]["somesection"]
^^^^^^^^
error TS2532: Object is possibly 'undefined'.
}
// But this works fine?
let resultB;
if (test["a-index"]) {
resultB = test["a-index"]["somesection"]
}
The only difference being that I assigned id to a variable. Even explicitly typing id as NodeId doesn't satisfy typescript.