Microsoft/TypeScript

Generic type gets widened in an unexpected way

Open

#52.249 aberto em 15 de jan. de 2023

Ver no GitHub
 (7 comments) (0 reactions) (0 assignees)TypeScript (6.726 forks)batch import
BugDomain: Mapped TypesHelp Wanted

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

Description

Bug Report

🔎 Search Terms

widen

🕗 Version & Regression Information

4.9.4

⏯ Playground Link

Playground link with relevant code

💻 Code

enum One {
  A = 'a',
  B = 'b',
  C = 'c'
}

const isOneSomethingMap = {
  [One.A]: true,
  [One.B]: false,
  [One.C]: true
} as const satisfies Record<One, boolean>;

type BooleanMapToUnion<T extends Record<string, boolean>> = {
  [P in keyof T]: T[P] extends true ? P : never;
}[keyof T];

type SomethingOne = BooleanMapToUnion<typeof isOneSomethingMap>;

const a = <T>(value: T) => value;
const b = <T>(fn: (value: T) => T, v: T): T => fn(v);

const v: SomethingOne = One.A as SomethingOne;
const v2: One.A | One.C = One.A as One.A | One.C;

const r1 = b(a, v); // One and not SomethingOne - not expected
const r2 = a(v); // SomethingOne as expected
const r3 = b(a, v2); // Union as expected

const r4 = b(a, One.A as One.A | One.C); // Works
const r5 = b(a, One.A as SomethingOne); // Still doesn't work...

🙁 Actual behavior

The type gets widened when using BooleanMapToUnion... I guess it is the culprit.

🙂 Expected behavior

Both union and using BooleanMapToUnion should work equally.

Guia do colaborador