microsoft / microsoft/TypeScript

Inferring rest of array in a conditional results in `unknown[]`

Open
#45,281 3 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

infer conditional array rest unknown

🕗 Version & Regression Information
  • I was unable to test this on prior versions than 4.1 because it uses template literal types added in 4.1
⏯ Playground Link

Playground link with relevant code

💻 Code

Using code from the #40336 description restricted to strings only:

type Join<S extends string[], D extends string> =
	S extends [] ? '' :
	S extends [string] ? `${S[0]}` :
	S extends [string, ...infer R] ? `${S[0]}${D}${Join<R, D>}` :
	string;
🙁 Actual behavior

R is inferred as being unknown[].

🙂 Expected behavior

R should be inferred as being string[].

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 reproduction and confirm that the rest parameter is inferred as unknown[] rather than string[]. Trace the compiler's conditional-type inference for the shown Join type, then verify that the rest is inferred as string[] without changing the expected behavior for other conditional 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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.