Skip to content

Type inference of 2 or more generic types from rest parameters being mapped type #47497

@Igorbek

Description

@Igorbek

Bug Report

🔎 Search Terms

  • infer multiple generic types from rest parameters being mapped type

🕗 Version & Regression Information

  • This is the behavior in every version I tried (4.3~4.4, 4.6 nightly), and I reviewed the FAQ for entries about type inference

⏯ Playground Link

Playground link with relevant code

💻 Code

// some generic type with 2 type parameters
type F<I, O> = { __type: (input: I) => O }

// this is what I want
declare function join<I, OS extends unknown[]>(...fs: { [K in keyof OS]: F<I, OS[K]> }): F<I, OS>

declare const f1: F<number, string>
declare const f2: F<number, boolean>

// (1)
// expected: join<number, [string, boolean, string]>
join(f1, f2, f1) // actual: join<unknown, [string, boolean, string]>, I type is inferred as unknown
//   ~~ Argument of type 'F<number, string>' is not assignable to parameter of type 'F<unknown, string>'.

// let's try with fixed tuple
declare function join2<I, OS extends readonly [unknown, unknown, unknown]>(...fs: [F<I, OS[0]>, F<I, OS[1]>, F<I, OS[2]>]): F<I, OS>

// (2)
// expected: join2<number, [string, boolean, string]>
join2(f1, f2, f1) // actual: join2<number, [unknown, unknown, unknown]>, I inferred correctly, but OS isn't

// not what I need, but let's try with only inferring from mapped type
declare function join3<OS extends unknown[]>(...fs: { [K in keyof OS]: F<number, OS[K]> }): F<number, OS>

// expected: join3<[string, boolean, string]>
join3(f1, f2, f1) // actual, as expected: join3<[string, boolean, string]>

🙁 Actual behavior

  • (1) call to be inferred as join<unknown, [string, boolean, string]>, could not infer I
  • (2) call to be inferred as join2<number, [unknown, unknown, unknown]>, could not infer OS

🙂 Expected behavior

  • (1) call to be inferred as join<number, [string, boolean, string]>
  • (2) call to be inferred as join2<number, [string, boolean, string]>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions