UNPKG

1.24 kBTypeScriptView Raw
1import type {Primitive} from './primitive';
2
3export type LiteralStringUnion<T> = LiteralUnion<T, string>;
4
5/**
6Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
7
8Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
9
10This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
11
12@example
13```
14import type {LiteralUnion} from 'type-fest';
15
16// Before
17
18type Pet = 'dog' | 'cat' | string;
19
20const pet: Pet = '';
21// Start typing in your TypeScript-enabled IDE.
22// You **will not** get auto-completion for `dog` and `cat` literals.
23
24// After
25
26type Pet2 = LiteralUnion<'dog' | 'cat', string>;
27
28const pet: Pet2 = '';
29// You **will** get auto-completion for `dog` and `cat` literals.
30```
31
32@category Type
33*/
34export type LiteralUnion<
35 LiteralType,
36 BaseType extends Primitive,
37> = LiteralType | (BaseType & Record<never, never>);