microsoft / microsoft/TypeScript

Indexing tuple intersection type beyond length produces unexpected type

Open
#42,557 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Intersection Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms

tuple intersection index type

⏯ Playground Link

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reproduce the tuple-intersection indexed access shown in the issue. Trace the compiler's type-checking path for out-of-bounds tuple indexing when an intersection is present, then add coverage showing that t[2] is typed as undefined while t[0] and t[1] retain their element types.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.