Skip to content

Rest element type not recognized for inner array types #45448

Open

Description

Bug Report

Hello,
The compiler doesn't recognize an inner array type as an "array type" when inferred in a conditional type.

🔎 Search Terms

I searched for array, rest element, inner array type, multidimensional array type.
Couldn't find anything.

⏯ Playground Link

Playground link with relevant code

💻 Code

The compiler complains with the following code:

type PushToTopArray<T, ArrayOfArray extends any[][]> =
	ArrayOfArray extends [infer TopArray, ...infer Rest] ?
		[[T, ...TopArray], ...Rest] :
		never;

🙁 Actual behavior

with error: A rest element type must be an array type. (2574) on ...TopArray.

🙂 Expected behavior

But since ArrayOfArray extends any[][], the inferred TopArray should be of type any[] and therefore satisfy the rest element type.

Activity

  1. RyanCavanaugh commented on Aug 13, 2021

    @RyanCavanaugh
    Member

    As a workaround, you can add this intersection:

    type PushToTopArray<T, ArrayOfArray extends any[][]> =
    	ArrayOfArray extends [infer TopArray, ...infer Rest] ?
    		[[T, ...(TopArray & unknown[])], ...Rest] :
    		never;
  2. miguel-leon commented on Aug 13, 2021

    @miguel-leon
    Author

    Thanks!

    I was using

    type PushToTopArray<T, ArrayOfArray extends any[][]> =
    	ArrayOfArray extends [infer TopArray, ...infer Rest] ?
    		TopArray extends any[] ?
    			[[T, ...TopArray], ...Rest] :
    			never : never;

    Is it the same, or one way is better/more proper?

    Edit: I think the intersection doesn't work quite as intended..

  3. RyanCavanaugh commented on Aug 13, 2021

    @RyanCavanaugh
    Member

    Probably yours is better if the intersection doesn't work 😅. I wasn't sure what the intended behavior of the alias was.

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

    BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional types

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions