microsoft / microsoft/TypeScript
Inferring rest of array in a conditional results in `unknown[]`
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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