Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inferring rest of array in a conditional results in unknown[] #45281

Open
avaly opened this issue Aug 2, 2021 · 3 comments
Open

Inferring rest of array in a conditional results in unknown[] #45281

avaly opened this issue Aug 2, 2021 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@avaly
Copy link

avaly commented Aug 2, 2021

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[].

@avaly avaly changed the title Infering rest of array in a conditional results in unknown[] Inferring rest of array in a conditional results in unknown[] Aug 2, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 2, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 2, 2021
@cypherfunc
Copy link

Has there been any movement on this?
The example can be simplified further, if it helps:

type Join<S extends string[]> =
    S extends [unknown, ...infer R] ? Join<R> // R is inferred as unknown[], but should be string[]
    : DoesntMatter;

I've been able to get around this by adding an extra conditional check, but it's awkward/confusing in the middle of more complex types:

type Join<S extends string[]> =
    S extends [unknown, ...infer R] ? Join<R extends string[] ? R : never>
    : DoesntMatter;

@nag
Copy link

nag commented Jun 6, 2022

Not only the rest of the array is misinferred, but any element as well:

type Foobar<T extends string[]> = T extends [infer F, ...infer R]
  ? [`${F}`, `${R[0]}`]
  : never

// Type 'F' is not assignable to type 'string | number | bigint | boolean | null | undefined'.

// Type 'R[0]' is not assignable to type 'string | number | bigint | boolean | null | undefined'.
// Type 'unknown' is not assignable to type 'string | number | bigint | boolean | null | undefined'.

@cypherfunc
Copy link

For any future folks, this is now easily solved with constraints in 4.7:
https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#extends-constraints-on-infer-type-variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants