Last change
on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | import {Primitive} from './basic';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | Allows 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.
|
---|
5 |
|
---|
6 | Currently, 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.
|
---|
7 |
|
---|
8 | This 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.
|
---|
9 |
|
---|
10 | @example
|
---|
11 | ```
|
---|
12 | import {LiteralUnion} from 'type-fest';
|
---|
13 |
|
---|
14 | // Before
|
---|
15 |
|
---|
16 | type Pet = 'dog' | 'cat' | string;
|
---|
17 |
|
---|
18 | const pet: Pet = '';
|
---|
19 | // Start typing in your TypeScript-enabled IDE.
|
---|
20 | // You **will not** get auto-completion for `dog` and `cat` literals.
|
---|
21 |
|
---|
22 | // After
|
---|
23 |
|
---|
24 | type Pet2 = LiteralUnion<'dog' | 'cat', string>;
|
---|
25 |
|
---|
26 | const pet: Pet2 = '';
|
---|
27 | // You **will** get auto-completion for `dog` and `cat` literals.
|
---|
28 | ```
|
---|
29 | */
|
---|
30 | export type LiteralUnion<
|
---|
31 | LiteralType,
|
---|
32 | BaseType extends Primitive
|
---|
33 | > = LiteralType | (BaseType & {_?: never});
|
---|
Note:
See
TracBrowser
for help on using the repository browser.