Skip to content

some types in 4.5.0 aren't correctly resolving to same types in 4.4.4 #46624

Closed
@suneettipirneni

Description

@suneettipirneni

Bug Report

I recently ran into the this strange error when I updated to typescript 4.5.0-beta, generic types that were resolving properly in 4.4.4, are not correctly resolving in 4.5.0-beta. Instead 4.5.0 returns a unions of all of the possible values while the same code in 4.4.4 returns a narrowed generic type.

🔎 Search Terms

4.5.0-beta, unions, generics

🕗 Version & Regression Information

typescript@beta, 4.5.0-beta, typescript@next

  • This changed between versions 4.4.4 and 4.5.0-beta

⏯ Playground Link

Same code for both versions:

Playground link with relevant code

💻 Code

export type kind = "one" | "two" | "three";

export interface Options<T> {
  item?: T;
  type?: kind;
}

type One = {};
type Two = {};
type Three = {};

type OneOptions = Options<One>;
type TwoOptions = Options<Two>;
type ThreeOption = Options<Three>;

interface MappedInterfaceOptions {
    "one": OneOptions;
    "two": TwoOptions;
    "three": ThreeOption;
}

export type InterfaceExtractor<
  T extends kind,
> = T extends kind
  ? MappedInterfaceOptions[T] extends Options<infer Item>
    ? Item
    : never
  : Options<One>;

export type Params<T extends kind> = { type?: T; } & Options<InterfaceExtractor<T>>;

declare function getInterfaceFromString<
  T extends kind,
>(options?: Params<T>): InterfaceExtractor<T>;

// Typescript 4.4.4 states the type is `Two`, 4.5.0 states the type is `One` | `Two` | `Three`
const result = getInterfaceFromString({ type: 'two' });

🙁 Actual behavior

In typescript 4.4.4 the type is correctly resolved to Two, whereas in typescript 4.5.0 the type is resolved to One | Two | Three which is incorrect.
4.4.4:
Screen Shot 2021-11-01 at 1 27 03 PM
4.5.0-beta:
Screen Shot 2021-11-01 at 1 27 22 PM

🙂 Expected behavior

The correct type for result in the playground and the codesample is Two, not One | Two | Three, typescript 4.5.0 should resolve to this type.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRecent RegressionThis is a new regression just found in the last major/minor version of TypeScript.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions