I defined two interfaces. The first one has an optional field, the second one has an index signature:
interface A {
foo?: { bar: number };
}
interface B {
[s: string]: { bar: number };
}
Why does the first interface give me a result of type number | undefined
when I access the property using optional chaining, whereas the second one gives only type number
?
const a: A = {};
const aa = a.foo?.bar;
// const aa: number | undefined
const b: B = {};
const bb = b.foo?.bar;
// const bb: number