UNPKG

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