Skip to content

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

Open

Description

@avaly

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

Activity

  1. changed the title [-]Infering rest of array in a conditional results in `unknown[]`[/-] [+]Inferring rest of array in a conditional results in `unknown[]`[/+] on Aug 2, 2021
  2. cypherfunc commented on Mar 31, 2022

    @cypherfunc

    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;
    
  3. nag commented on Jun 6, 2022

    @nag

    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'.
  4. cypherfunc commented on Jun 8, 2022

    @cypherfunc
  5. added
    Domain: check: Type Inference
    on Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: check: Type Inference

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions