HomeSegun Adebayo

Literal Union

Loosen and strictify a literal union type.

  • #typescript
type Loose<T> = T | (string & {});

type Strictify<T extends string> = T extends `${infer _}` ? T : never;

type DemoA = Loose<'red' | 'blue' | 'green'>;
// 'red' | 'blue' | 'green' | (string & {})

type DemoB = Strictify<DemoA>;
// 'red' | 'blue' | 'green'

Tweet this snippet

Edit on github

Segun Adebayo

Passionate UI engineer looking to bridge the gap between design and code

All rights reserved © Segun Adebayo 2024