Skip to content

Inferring less information for generics in 4.2+ compared to previous versions #45471

@Haroenv

Description

@Haroenv

Bug Report

infer used as the value of a generic no longer infers the actual value, but it infers the default value of that generic argument.

🔎 Search Terms

infer, unknown, default, generic

possibly similar issues?

#43161
#42939

🕗 Version & Regression Information

  • This changed between versions 4.1.5 and 4.2.3

⏯ Playground Link

Playground link with relevant code in 4.2.3
Playground link with relevant code in 4.1.5

💻 Code

type Shape = {
  key: Record<string, unknown>;
};

type ShapeCreator<T extends Shape> = (cb: (state: T['key']) => void) => void;

type ExtractShape<T> = T extends ShapeCreator<infer TShape> ? TShape : never;

type StateShape = {
  key: { letter: string };
};
type StateShapeCreator = ShapeCreator<StateShape>;

const wrongShape: ExtractShape<StateShapeCreator> = {
  key: {
    // @ts-expect-error
    letter: 2,
  },
};

🙁 Actual behavior

the inferred value is the default of the generic

🙂 Expected behavior

the inferred value is the actual value of the generic, like it used to do.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions