-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathindex.d.ts
154 lines (138 loc) · 4.54 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as React from 'react';
// disable automatic export
export {};
/**
* `T extends ConsistentWith<T, U>` means that where `T` has overlapping properties with
* `U`, their value types do not conflict.
*
* @internal
*/
export type ConsistentWith<DecorationTargetProps, InjectedProps> = {
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
? InjectedProps[P] extends DecorationTargetProps[P]
? DecorationTargetProps[P]
: InjectedProps[P]
: DecorationTargetProps[P];
};
/**
* a function that takes {component} and returns a component that passes along
* all the props to {component} except the {InjectedProps} and will accept
* additional {AdditionalProps}
*/
export type PropInjector<InjectedProps, AdditionalProps = {}> = <
C extends React.JSXElementConstructor<ConsistentWith<React.ComponentProps<C>, InjectedProps>>,
>(
component: C,
) => React.JSXElementConstructor<
DistributiveOmit<
React.JSX.LibraryManagedAttributes<C, React.ComponentProps<C>>,
keyof InjectedProps
> &
AdditionalProps
>;
/**
* Remove properties `K` from `T`.
* Distributive for union types.
*
* @internal
*/
export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
/**
* Generate a set of string literal types with the given default record `T` and
* override record `U`.
*
* If the property value was `true`, the property key will be added to the
* string union.
*
* @internal
*/
export type OverridableStringUnion<T extends string | number, U = {}> = GenerateStringUnion<
Overwrite<Record<T, true>, U>
>;
/**
* Like `T & U`, but using the value types from `U` where their properties overlap.
*
* @internal
*/
export type Overwrite<T, U> = DistributiveOmit<T, keyof U> & U;
type GenerateStringUnion<T> = Extract<
{
[Key in keyof T]: true extends T[Key] ? Key : never;
}[keyof T],
string
>;
// https://stackoverflow.com/questions/53807517/how-to-test-if-two-types-are-exactly-the-same
export type IfEquals<T, U, Y = unknown, N = never> =
(<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? Y : N;
/**
* Issues a type error if `Expected` is not identical to `Actual`.
*
* `Expected` should be declared when invoking `expectType`.
* `Actual` should almost always we be a `typeof value` statement.
*
* @example `expectType<number | string, typeof value>(value)`
* TypeScript issues a type error since `value is not assignable to never`.
* This means `typeof value` is not identical to `number | string`
* @param actual
*/
export function expectType<Expected, Actual>(actual: IfEquals<Actual, Expected, Actual>): void;
/**
* A component whose root component can be controlled via a `component` prop.
*
* Adjusts valid props based on the type of `component`.
*/
export interface OverridableComponent<M extends OverridableTypeMap> {
// If you make any changes to this interface, please make sure to update the
// `OverridableComponent` type in `mui-material/src/OverridableComponent.d.ts` as well.
// Also, there are types in Base UI that have a similar shape to this interface
// (for example SelectType, OptionType, etc.).
<C extends React.ElementType>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: C;
} & OverrideProps<M, C>,
): React.JSX.Element | null;
(props: DefaultComponentProps<M>): React.JSX.Element | null;
propTypes?: any;
}
/**
* Props of the component if `component={Component}` is used.
*/
// prettier-ignore
export type OverrideProps<
M extends OverridableTypeMap,
C extends React.ElementType
> = (
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithRef<C>, keyof BaseProps<M>>
);
/**
* Props if `component={Component}` is NOT used.
*/
// prettier-ignore
export type DefaultComponentProps<M extends OverridableTypeMap> =
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithRef<M['defaultComponent']>, keyof BaseProps<M>>;
/**
* Props defined on the component.
*/
// prettier-ignore
export type BaseProps<M extends OverridableTypeMap> = M['props'];
export interface OverridableTypeMap {
props: {};
defaultComponent: React.ElementType;
}
/**
* Simplifies the display of a type (without modifying it).
* Taken from https://effectivetypescript.com/2022/02/25/gentips-4-display/
*/
export type Simplify<T> = T extends Function ? T : { [K in keyof T]: T[K] };
/**
* Changes the properties K from T to required
*/
export type PartiallyRequired<T, K extends keyof T> = DistributiveOmit<T, K> & {
[P in K]-?: T[P];
};