UNPKG

8.24 kBTypeScriptView Raw
1export = preact;
2export as namespace preact;
3
4import { JSXInternal } from './jsx';
5
6declare namespace preact {
7 export import JSX = JSXInternal;
8
9 //
10 // Preact Virtual DOM
11 // -----------------------------------
12
13 interface VNode<P = {}> {
14 type: ComponentType<P> | string;
15 props: P & { children: ComponentChildren };
16 key: Key;
17 /**
18 * ref is not guaranteed by React.ReactElement, for compatibility reasons
19 * with popular react libs we define it as optional too
20 */
21 ref?: Ref<any> | null;
22 /**
23 * The time this `vnode` started rendering. Will only be set when
24 * the devtools are attached.
25 * Default value: `0`
26 */
27 startTime?: number;
28 /**
29 * The time that the rendering of this `vnode` was completed. Will only be
30 * set when the devtools are attached.
31 * Default value: `-1`
32 */
33 endTime?: number;
34 }
35
36 //
37 // Preact Component interface
38 // -----------------------------------
39
40 type Key = string | number | any;
41
42 type RefObject<T> = { current?: T | null };
43 type RefCallback<T> = (instance: T | null) => void;
44 type Ref<T> = RefObject<T> | RefCallback<T>;
45
46 type ComponentChild =
47 | VNode<any>
48 | object
49 | string
50 | number
51 | boolean
52 | null
53 | undefined;
54 type ComponentChildren = ComponentChild[] | ComponentChild;
55
56 interface Attributes {
57 key?: Key;
58 jsx?: boolean;
59 }
60
61 interface ClassAttributes<T> extends Attributes {
62 ref?: Ref<T>;
63 }
64
65 interface PreactDOMAttributes {
66 children?: ComponentChildren;
67 dangerouslySetInnerHTML?: {
68 __html: string;
69 };
70 }
71
72 type RenderableProps<P, RefType = any> = P &
73 Readonly<Attributes & { children?: ComponentChildren; ref?: Ref<RefType> }>;
74
75 type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
76 type ComponentFactory<P = {}> = ComponentType<P>;
77
78 type ComponentProps<
79 C extends ComponentType<any> | keyof JSXInternal.IntrinsicElements
80 > = C extends ComponentType<infer P>
81 ? P
82 : C extends keyof JSXInternal.IntrinsicElements
83 ? JSXInternal.IntrinsicElements[C]
84 : never;
85
86 interface FunctionComponent<P = {}> {
87 (props: RenderableProps<P>, context?: any): VNode<any> | null;
88 displayName?: string;
89 defaultProps?: Partial<P>;
90 }
91 interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
92
93 interface ComponentClass<P = {}, S = {}> {
94 new (props: P, context?: any): Component<P, S>;
95 displayName?: string;
96 defaultProps?: Partial<P>;
97 getDerivedStateFromProps?(
98 props: Readonly<P>,
99 state: Readonly<S>
100 ): Partial<S> | null;
101 getDerivedStateFromError?(error: any): Partial<S> | null;
102 }
103 interface ComponentConstructor<P = {}, S = {}> extends ComponentClass<P, S> {}
104
105 // Type alias for a component instance considered generally, whether stateless or stateful.
106 type AnyComponent<P = {}, S = {}> = FunctionComponent<P> | Component<P, S>;
107
108 interface Component<P = {}, S = {}> {
109 componentWillMount?(): void;
110 componentDidMount?(): void;
111 componentWillUnmount?(): void;
112 getChildContext?(): object;
113 componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
114 shouldComponentUpdate?(
115 nextProps: Readonly<P>,
116 nextState: Readonly<S>,
117 nextContext: any
118 ): boolean;
119 componentWillUpdate?(
120 nextProps: Readonly<P>,
121 nextState: Readonly<S>,
122 nextContext: any
123 ): void;
124 getSnapshotBeforeUpdate?(oldProps: Readonly<P>, oldState: Readonly<S>): any;
125 componentDidUpdate?(
126 previousProps: Readonly<P>,
127 previousState: Readonly<S>,
128 snapshot: any
129 ): void;
130 componentDidCatch?(error: any, errorInfo: any): void;
131 }
132
133 abstract class Component<P, S> {
134 constructor(props?: P, context?: any);
135
136 static displayName?: string;
137 static defaultProps?: any;
138 static contextType?: Context<any>;
139
140 // Static members cannot reference class type parameters. This is not
141 // supported in TypeScript. Reusing the same type arguments from `Component`
142 // will lead to an impossible state where one cannot satisfy the type
143 // constraint under no circumstances, see #1356.In general type arguments
144 // seem to be a bit buggy and not supported well at the time of this
145 // writing with TS 3.3.3333.
146 static getDerivedStateFromProps?(
147 props: Readonly<object>,
148 state: Readonly<object>
149 ): object | null;
150 static getDerivedStateFromError?(error: any): object | null;
151
152 state: Readonly<S>;
153 props: RenderableProps<P>;
154 context: any;
155 base?: Element | Text;
156
157 // From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e836acc75a78cf0655b5dfdbe81d69fdd4d8a252/types/react/index.d.ts#L402
158 // // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
159 // // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
160 setState<K extends keyof S>(
161 state:
162 | ((
163 prevState: Readonly<S>,
164 props: Readonly<P>
165 ) => Pick<S, K> | Partial<S> | null)
166 | (Pick<S, K> | Partial<S> | null),
167 callback?: () => void
168 ): void;
169
170 forceUpdate(callback?: () => void): void;
171
172 abstract render(
173 props?: RenderableProps<P>,
174 state?: Readonly<S>,
175 context?: any
176 ): ComponentChild;
177 }
178
179 //
180 // Preact createElement
181 // -----------------------------------
182
183 function createElement(
184 type: string,
185 props:
186 | (JSXInternal.HTMLAttributes &
187 JSXInternal.SVGAttributes &
188 Record<string, any>)
189 | null,
190 ...children: ComponentChildren[]
191 ): VNode<any>;
192 function createElement<P>(
193 type: ComponentType<P>,
194 props: (Attributes & P) | null,
195 ...children: ComponentChildren[]
196 ): VNode<any>;
197 namespace createElement {
198 export import JSX = JSXInternal;
199 }
200
201 function h(
202 type: string,
203 props:
204 | (JSXInternal.HTMLAttributes &
205 JSXInternal.SVGAttributes &
206 Record<string, any>)
207 | null,
208 ...children: ComponentChildren[]
209 ): VNode<any>;
210 function h<P>(
211 type: ComponentType<P>,
212 props: (Attributes & P) | null,
213 ...children: ComponentChildren[]
214 ): VNode<any>;
215 namespace h {
216 export import JSX = JSXInternal;
217 }
218
219 //
220 // Preact render
221 // -----------------------------------
222
223 function render(
224 vnode: ComponentChild,
225 parent: Element | Document | ShadowRoot | DocumentFragment,
226 replaceNode?: Element | Text
227 ): void;
228 function hydrate(
229 vnode: ComponentChild,
230 parent: Element | Document | ShadowRoot | DocumentFragment
231 ): void;
232 function cloneElement(
233 vnode: VNode<any>,
234 props?: any,
235 ...children: ComponentChildren[]
236 ): VNode<any>;
237 function cloneElement<P>(
238 vnode: VNode<P>,
239 props?: any,
240 ...children: ComponentChildren[]
241 ): VNode<P>;
242
243 //
244 // Preact Built-in Components
245 // -----------------------------------
246
247 // TODO: Revisit what the public type of this is...
248 const Fragment: ComponentClass<{}, {}>;
249
250 //
251 // Preact options
252 // -----------------------------------
253
254 /**
255 * Global options for preact
256 */
257 interface Options {
258 /** Attach a hook that is invoked whenever a VNode is created. */
259 vnode?(vnode: VNode): void;
260 /** Attach a hook that is invoked immediately before a vnode is unmounted. */
261 unmount?(vnode: VNode): void;
262 /** Attach a hook that is invoked after a vnode has rendered. */
263 diffed?(vnode: VNode): void;
264 event?(e: Event): any;
265 requestAnimationFrame?: typeof requestAnimationFrame;
266 debounceRendering?(cb: () => void): void;
267 useDebugValue?(value: string | number): void;
268 __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
269 // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
270 }
271
272 const options: Options;
273
274 //
275 // Preact helpers
276 // -----------------------------------
277 function createRef<T = any>(): RefObject<T>;
278 function toChildArray(
279 children: ComponentChildren
280 ): Array<VNode | string | number>;
281 function isValidElement(vnode: any): vnode is VNode;
282
283 //
284 // Context
285 // -----------------------------------
286 interface Consumer<T>
287 extends FunctionComponent<{
288 children: (value: T) => ComponentChildren;
289 }> {}
290 interface PreactConsumer<T> extends Consumer<T> {}
291
292 interface Provider<T>
293 extends FunctionComponent<{
294 value: T;
295 children: ComponentChildren;
296 }> {}
297 interface PreactProvider<T> extends Provider<T> {}
298
299 interface Context<T> {
300 Consumer: Consumer<T>;
301 Provider: Provider<T>;
302 }
303 interface PreactContext<T> extends Context<T> {}
304
305 function createContext<T>(defaultValue: T): Context<T>;
306}