Skip to content

...infer X extends TypeParameter[] in conditional type inappropriate bindingΒ #54197

Closed
@webstrand

Description

@webstrand

Bug Report

πŸ”Ž Search Terms

conditional type, extends, infer, spread, tuple

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about extends

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Foo<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly unknown[]] ? X : never;
type Bar<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly K[]] ? X : never;
type Baz<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly (K | "a" | "b")[]] ? X : never;

type x = Foo<"a" | "b", ["a", "b", "b"]>
//   ^? - type x = ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]> 
//   ^? - type y = readonly ("a" | "b")[]
type z = Baz<"a" | "b", ["a", "b", "b"]>
//   ^? - type z = ["b", "b"]

πŸ™ Actual behavior

The conditional types do not produce the same output:

type x = Foo<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]> // readonly ("a" | "b")[]
type z = Baz<"a" | "b", ["a", "b", "b"]> // ["b", "b"]

πŸ™‚ Expected behavior

The conditional types should all produce the same output. The fact that K is a type parameter should not affect the output.

type x = Foo<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type z = Baz<"a" | "b", ["a", "b", "b"]> // ["b", "b"]

Alternatively if that is infeasible for technical reasons, the conditional type should fail. Inferring the constraint makes traversing the tuple impossible.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions