How could one narrow/split/decompose a possibly discriminated union type?
For example in the following I’d like to get the type with the kind: "bar" from MyUnion.
type MyUnion = { kind: "foo", foo: number } | { kind: "bar", bar: string };
// Here I want to somehow get the type { kind: "bar", bar: string } from MyUnion
type Narrowed = NarrowUnion<MyUnion, { kind: "bar" }>;
>Solution :
Unless you have some use case not mentioned in the question, you can just use the provided Extract<T, U> utility type:
type Narrowed = Extract<MyUnion, { kind: "bar" }>;
/* type Narrowed = { kind: "bar"; bar: string;}