Skip to content

Regression in 2.9+: expansion of type operator is incorrect #24640

Closed
@pelotom

Description

@pelotom

TypeScript Version: 2.9.1, 3.0.0-dev.20180602

Search Terms: conditional union type operator expansion

Code

type U =
  | {
      tag: 'a';
      foo: number;
      f(): void;
    }
  | {
      tag: 'b';
      bar: boolean;
      g(): void;
    };

type FilterUnion<T extends { tag: string }, K> = T extends { tag: K } ? T : never;
type StripFunctions<T> = Pick<
  T,
  { [K in keyof T]: T[K] extends (...args: any[]) => any ? never : K }[keyof T]
>;
type FilterAndStrip<K extends U['tag']> = StripFunctions<FilterUnion<U, K>>;

type X1 = FilterAndStrip<'a'>;
type X2 = StripFunctions<FilterUnion<U, 'a'>>;

Expected behavior:
X1 and X2 should both have the type { tag: 'a'; foo: number }, since the latter is just an expansion of the first.

Actual behavior:

X2 is correct, but X1: {}.

Playground Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions