microsoft / microsoft/TypeScript

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

Open
#45,471 0 comments 3 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Aug 17, 2021.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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?

https://github.com/microsoft/TypeScript/issues/43161
https://github.com/microsoft/TypeScript/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.