UNPKG

2.35 kBTypeScriptView Raw
1import type {ConditionalSimplifyDeep} from './conditional-simplify';
2import type {NonRecursiveType} from './internal';
3
4/**
5Deeply simplifies an object type.
6
7You can exclude certain types from being simplified by providing them in the second generic `ExcludeType`.
8
9Useful to flatten the type output to improve type hints shown in editors.
10
11@example
12```
13import type {SimplifyDeep} from 'type-fest';
14
15type PositionX = {
16 left: number;
17 right: number;
18};
19
20type PositionY = {
21 top: number;
22 bottom: number;
23};
24
25type Properties1 = {
26 height: number;
27 position: PositionY;
28};
29
30type Properties2 = {
31 width: number;
32 position: PositionX;
33};
34
35type Properties = Properties1 & Properties2;
36// In your editor, hovering over `Props` will show the following:
37//
38// type Properties = Properties1 & Properties2;
39
40type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;
41// But if wrapped in SimplifyDeep, hovering over `SimplifyDeepProperties` will show a flattened object with all the properties:
42//
43// SimplifyDeepProperties = {
44// height: number;
45// width: number;
46// position: {
47// top: number;
48// bottom: number;
49// left: number;
50// right: number;
51// };
52// };
53```
54
55@example
56```
57import type {SimplifyDeep} from 'type-fest';
58
59// A complex type that you don't want or need to simplify
60type ComplexType = {
61 a: string;
62 b: 'b';
63 c: number;
64 ...
65};
66
67type PositionX = {
68 left: number;
69 right: number;
70};
71
72type PositionY = {
73 top: number;
74 bottom: number;
75};
76
77// You want to simplify all other types
78type Properties1 = {
79 height: number;
80 position: PositionY;
81 foo: ComplexType;
82};
83
84type Properties2 = {
85 width: number;
86 position: PositionX;
87 foo: ComplexType;
88};
89
90type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2, ComplexType>;
91// If wrapped in `SimplifyDeep` and set `ComplexType` to exclude, hovering over `SimplifyDeepProperties` will
92// show a flattened object with all the properties except `ComplexType`:
93//
94// SimplifyDeepProperties = {
95// height: number;
96// width: number;
97// position: {
98// top: number;
99// bottom: number;
100// left: number;
101// right: number;
102// };
103// foo: ComplexType;
104// };
105```
106
107@see Simplify
108@category Object
109*/
110export type SimplifyDeep<Type, ExcludeType = never> =
111 ConditionalSimplifyDeep<
112 Type,
113 ExcludeType | NonRecursiveType | Set<unknown> | Map<unknown, unknown>,
114 object
115 >;