microsoft / microsoft/TypeScript

`never` as keyof tuples/arrays behaves the same as `number`

Open
#55,841 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Indexed Access Types Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

tuple keyof never, tuple[never], array keyof never

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this issue

Versions I tried:

  • 5.2.2
  • nightly (playground)
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230923#code/C4TwDgpgBAglC8UDeBfA2gOwgNwgJwF0oB6YqQUHIotc8AoUSKAIQWSgEMAuKAZ2DwCWGAOZR01fEVIUqOfPXDQAwqzQFMcwiTKUJdBtAAiqvoJHq9UsoBlyXvyHCFjAKKtTDtRppWot9yKdoADFWAAp-UQAfKgBXAFsAI3wASk9LKG1fOzMo2MT8IA

💻 Code
type A = {}[never] // ✅ never
type B = { a: string }[never] // ✅ never
type C = [][never] // ✅ never
type D = [string][never] // ❌ string
type E = string[][never] // ❌ string
type F = (string | number)[][never]  // ❌ string | number
🙁 Actual behavior

After using a never key on tuples/arrays you get back the entirety of the tuple/array elements type.

🙂 Expected behavior

After using a never key on tuples/arrays you get back never, as when dealing with objects.

Additional information about the issue

Not sure if this has historical reasons to behave like it does, but couldn't find any specific info about it.

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 by reproducing the behavior in the linked TypeScript Playground, comparing the object, tuple, and array examples using never. Trace the compiler's indexed-access handling for tuple and array keys; done means those examples evaluate to never consistently with object indexing, with appropriate regression coverage.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.