-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.d.ts
307 lines (256 loc) · 8.84 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
export interface VNode<P = {}> {
type: ComponentType<P> | string;
props: P & { children: ComponentChildren };
key: Key;
/**
* ref is not guaranteed by React.ReactElement, for compatibility reasons
* with popular react libs we define it as optional too
*/
ref?: Ref<any> | null;
/**
* The time this `vnode` started rendering. Will only be set when
* the devtools are attached.
* Default value: `0`
*/
startTime?: number;
/**
* The time that the rendering of this `vnode` was completed. Will only be
* set when the devtools are attached.
* Default value: `-1`
*/
endTime?: number;
}
export type Key = string | number | any;
export type RefObject<T> = { current: T | null };
export type RefCallback<T> = (instance: T | null) => void;
export type Ref<T> = RefObject<T> | RefCallback<T>;
export type ComponentChild =
| VNode<any>
| object
| string
| number
| bigint
| boolean
| null
| undefined;
export type ComponentChildren = ComponentChild[] | ComponentChild;
export interface Attributes {
key?: Key | undefined;
jsx?: boolean | undefined;
}
export interface ClassAttributes<T> extends Attributes {
ref?: Ref<T>;
}
export interface PreactDOMAttributes {
children?: ComponentChildren;
dangerouslySetInnerHTML?: {
__html: string;
};
}
export interface ErrorInfo {
componentStack?: string;
}
export type RenderableProps<P, RefType = any> = P &
Readonly<Attributes & { children?: ComponentChildren; ref?: Ref<RefType> }>;
export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
export type ComponentFactory<P = {}> = ComponentType<P>;
export type ComponentProps<
C extends ComponentType<any> | keyof JSX.IntrinsicElements
> = C extends ComponentType<infer P>
? P
: C extends keyof JSX.IntrinsicElements
? JSX.IntrinsicElements[C]
: never;
export interface FunctionComponent<P = {}> {
(props: RenderableProps<P>, context?: any): VNode<any> | null;
displayName?: string;
defaultProps?: Partial<P> | undefined;
}
export interface FunctionalComponent<P = {}> extends FunctionComponent<P> { }
export interface ComponentClass<P = {}, S = {}> {
new(props: P, context?: any): Component<P, S>;
displayName?: string;
defaultProps?: Partial<P>;
contextType?: Context<any>;
getDerivedStateFromProps?(
props: Readonly<P>,
state: Readonly<S>
): Partial<S> | null;
getDerivedStateFromError?(error: any): Partial<S> | null;
}
export interface ComponentConstructor<P = {}, S = {}>
extends ComponentClass<P, S> { }
// Type alias for a component instance considered generally, whether stateless or stateful.
export type AnyComponent<P = {}, S = {}> =
| FunctionComponent<P>
| Component<P, S>;
export interface Component<P = {}, S = {}> {
componentWillMount?(): void;
componentDidMount?(): void;
componentWillUnmount?(): void;
getChildContext?(): object;
componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
shouldComponentUpdate?(
nextProps: Readonly<P>,
nextState: Readonly<S>,
nextContext: any
): boolean;
componentWillUpdate?(
nextProps: Readonly<P>,
nextState: Readonly<S>,
nextContext: any
): void;
getSnapshotBeforeUpdate?(oldProps: Readonly<P>, oldState: Readonly<S>): any;
componentDidUpdate?(
previousProps: Readonly<P>,
previousState: Readonly<S>,
snapshot: any
): void;
componentDidCatch?(error: any, errorInfo: ErrorInfo): void;
}
export abstract class Component<P, S> {
constructor(props?: P, context?: any);
static displayName?: string;
static defaultProps?: any;
static contextType?: Context<any>;
// Static members cannot reference class type parameters. This is not
// supported in TypeScript. Reusing the same type arguments from `Component`
// will lead to an impossible state where one cannot satisfy the type
// constraint under no circumstances, see #1356.In general type arguments
// seem to be a bit buggy and not supported well at the time of this
// writing with TS 3.3.3333.
static getDerivedStateFromProps?(
props: Readonly<object>,
state: Readonly<object>
): object | null;
static getDerivedStateFromError?(error: any): object | null;
state: Readonly<S>;
props: RenderableProps<P>;
context: any;
base?: Element | Text;
// From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e836acc75a78cf0655b5dfdbe81d69fdd4d8a252/types/react/index.d.ts#L402
// // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
// // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
setState<K extends keyof S>(
state:
| ((
prevState: Readonly<S>,
props: Readonly<P>
) => Pick<S, K> | Partial<S> | null)
| (Pick<S, K> | Partial<S> | null),
callback?: () => void
): void;
forceUpdate(callback?: () => void): void;
abstract render(
props?: RenderableProps<P>,
state?: Readonly<S>,
context?: any
): ComponentChild;
}
//
// Preact render
// -----------------------------------
interface ContainerNode {
readonly nodeType: number;
readonly parentNode: ContainerNode | null;
readonly firstChild: ContainerNode | null;
readonly childNodes: ArrayLike<ContainerNode>;
insertBefore(node: ContainerNode, child: ContainerNode | null): ContainerNode;
appendChild(node: ContainerNode): ContainerNode;
removeChild(child: ContainerNode): ContainerNode;
}
// export function render(vnode: ComponentChild, parent: ContainerNode): void;
/**
* Global options for preact
*/
export interface Options {
/** Attach a hook that is invoked whenever a VNode is created. */
vnode?(vnode: VNode): void;
/** Attach a hook that is invoked immediately before a vnode is unmounted. */
unmount?(vnode: VNode): void;
/** Attach a hook that is invoked after a vnode has rendered. */
diffed?(vnode: VNode): void;
event?(e: Event): any;
requestAnimationFrame?(callback: () => void): void;
debounceRendering?(cb: () => void): void;
useDebugValue?(value: string | number): void;
_addHookName?(name: string | number): void;
__suspenseDidResolve?(vnode: VNode, cb: () => void): void;
// __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
/**
* Customize attribute serialization when a precompiled JSX transform
* is used.
*/
attr?(name: string, value: any): string | void;
}
export const options: Options;
//
// Context
// -----------------------------------
export interface Consumer<T>
extends FunctionComponent<{
children: (value: T) => ComponentChildren;
}> { }
export interface PreactConsumer<T> extends Consumer<T> { }
export interface Provider<T>
extends FunctionComponent<{
value: T;
children?: ComponentChildren;
}> { }
export interface PreactProvider<T> extends Provider<T> { }
export type ContextType<C extends Context<any>> = C extends Context<infer T>
? T
: never;
export interface Context<T> {
Consumer: Consumer<T>;
Provider: Provider<T>;
displayName?: string;
}
export interface PreactContext<T> extends Context<T> { }
export function createContext<T>(defaultValue: T): Context<T>;
export const Fragment: FunctionComponent<{}>;
export function createElement(
type: ComponentType<any>,
props: any,
...children: ComponentChildren[]
): VNode;
declare type HFunction = {
(tag: string, props?: Record<string, any>, ...children: (VNode | string)[]): VNode;
(tag: Function, props?: Record<string, any>, ...children: (VNode | string)[]): VNode;
};
declare const h: HFunction;
// declare namespace h {
// namespace JSX {
// interface Element extends VNode<any> { }
// }
// }
// Component type
// type Component = (props: Record<string, any>) => VNode;
// RenderFunction type
type RenderFunction = {
(element: VNode, container: Element | null): void;
(component: Component, container: Element | null, props?: Record<string, any>): void;
};
// Declare the render function
export const render: RenderFunction;
export function cloneElement(
vnode: VNode<any>,
props?: any,
...children: ComponentChildren[]
): VNode<any>;
export function cloneElement<P>(
vnode: VNode<P>,
props?: any,
...children: ComponentChildren[]
): VNode<P>;
export { h, HFunction };
export namespace options { }
export default options;
//
// Preact helpers
// -----------------------------------
export function createRef<T = any>(): RefObject<T>;
export function toChildArray(
children: ComponentChildren
): Array<VNode | string | number>;
export const isValidElement: (v: VNode) => boolean;