Microsoft/TypeScript
GitHub ã§èŠãIndexing tuple intersection type beyond length produces unexpected type
Open
#42,557 opened on 2021幎1æ30æ¥
BugDomain: IntersectionHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
Bug Report
ð Search Terms
tuple intersection index type
⯠Playground Link
ð» Code
type T = [number, boolean] & { x: string };
declare const t: T;
const a = t[0]; // number
const b = t[1]; // boolean
const c = t[2]; // number | boolean !?
ð Actual behavior
c is typed as number | boolean
ð Expected behavior
c should be typed as undefined
I'm not sure if this is a bug, or intended behavior. But it was surprising. If the type is just a tuple, indexing beyond its length produced undefined. But if it is part of an intersection, the element type is changed to number | boolean, as if it is no longer a tuple, but an array. However, indexing 0 or 1 still produces the correct type for those element positions.