Description
Bug Report
Let me preface this by saying that I realize that this is likely a conscious design decision (seeing as this behavior seems to have existed ever since noUncheckedIndexedAccess
was introduced). I do think it's highly undesirable though. And sorry in advance if this has already been answered (although I didn't find anything through search)
🔎 Search Terms
noUncheckedIndexedAccess
🕗 Version & Regression Information
It seems like this has been the behavior ever since noUnchekedIndexedAccess
was introduced.
⏯ Playground Link
💻 Code
type A = string[];
/** These are type errors but shouldn't be, as `A[number]` should include `undefined` */
const x: A[number] = undefined;
const y: A[0] = undefined;
const z: A[1000] = undefined;
The following illustrates how authors might unintentionally introduce unsoundness by including a return type annotation:
/** Without return type annotation */
const get = <O, K extends keyof O>(o: O, k: K) => o[k];
/** With return type annotation */
const get2 = <O, K extends keyof O>(o: O, k: K): O[K] => o[k];
/** A type error as expected */
const a: number = get([1], 1);
/** No type error, because `get2` happens to include a return type annotation */
const b: number = get2([1], 1);
🙁 Actual behavior
When doing indexed access on array types (f. ex. (number[])[number])
), the resulting type does not include undefined
. This can erode the type safety that noUncheckedIndexedAccess
provides, without the author realizing it.
This is also the case for Record
but I haven't included an example for that.
🙂 Expected behavior
Property access on an indexed type should resolve to a type that includes undefined