Closed
Description
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:
4.5.0-beta:
🙂 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.