UNPKG

10.2 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;
57 jsx?: boolean;
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>;
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<any>;
198export function createElement<
199 P extends JSXInternal.HTMLAttributes<T>,
200 T extends HTMLElement
201>(
202 type: keyof JSXInternal.IntrinsicElements,
203 props: (ClassAttributes<T> & P) | null,
204 ...children: ComponentChildren[]
205): VNode<any>;
206export function createElement<
207 P extends JSXInternal.SVGAttributes<T>,
208 T extends HTMLElement
209>(
210 type: keyof JSXInternal.IntrinsicElements,
211 props: (ClassAttributes<T> & P) | null,
212 ...children: ComponentChildren[]
213): VNode<any>;
214export function createElement<T extends HTMLElement>(
215 type: string,
216 props:
217 | (ClassAttributes<T> &
218 JSXInternal.HTMLAttributes &
219 JSXInternal.SVGAttributes)
220 | null,
221 ...children: ComponentChildren[]
222): VNode<any>;
223export function createElement<P>(
224 type: ComponentType<P>,
225 props: (Attributes & P) | null,
226 ...children: ComponentChildren[]
227): VNode<any>;
228export namespace createElement {
229 export import JSX = JSXInternal;
230}
231
232export function h(
233 type: 'input',
234 props:
235 | (JSXInternal.DOMAttributes<HTMLInputElement> &
236 ClassAttributes<HTMLInputElement>)
237 | null,
238 ...children: ComponentChildren[]
239): VNode<any>;
240export function h<
241 P extends JSXInternal.HTMLAttributes<T>,
242 T extends HTMLElement
243>(
244 type: keyof JSXInternal.IntrinsicElements,
245 props: (ClassAttributes<T> & P) | null,
246 ...children: ComponentChildren[]
247): VNode<any>;
248export function h<
249 P extends JSXInternal.SVGAttributes<T>,
250 T extends HTMLElement
251>(
252 type: keyof JSXInternal.IntrinsicElements,
253 props: (ClassAttributes<T> & P) | null,
254 ...children: ComponentChildren[]
255): VNode<any>;
256export function h<T extends HTMLElement>(
257 type: string,
258 props:
259 | (ClassAttributes<T> &
260 JSXInternal.HTMLAttributes &
261 JSXInternal.SVGAttributes)
262 | null,
263 ...children: ComponentChildren[]
264): VNode<any>;
265export function h<P>(
266 type: ComponentType<P>,
267 props: (Attributes & P) | null,
268 ...children: ComponentChildren[]
269): VNode<any>;
270export namespace h {
271 export import JSX = JSXInternal;
272}
273
274//
275// Preact render
276// -----------------------------------
277
278export function render(
279 vnode: ComponentChild,
280 parent: Element | Document | ShadowRoot | DocumentFragment
281): void;
282/**
283 * @deprecated Will be removed in v11.
284 *
285 * Replacement Preact 10+ implementation can be found here: https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c
286 */
287export function render(
288 vnode: ComponentChild,
289 parent: Element | Document | ShadowRoot | DocumentFragment,
290 replaceNode?: Element | Text
291): void;
292export function hydrate(
293 vnode: ComponentChild,
294 parent: Element | Document | ShadowRoot | DocumentFragment
295): void;
296export function cloneElement(
297 vnode: VNode<any>,
298 props?: any,
299 ...children: ComponentChildren[]
300): VNode<any>;
301export function cloneElement<P>(
302 vnode: VNode<P>,
303 props?: any,
304 ...children: ComponentChildren[]
305): VNode<P>;
306
307//
308// Preact Built-in Components
309// -----------------------------------
310
311// TODO: Revisit what the public type of this is...
312export const Fragment: FunctionComponent<{}>;
313
314//
315// Preact options
316// -----------------------------------
317
318/**
319 * Global options for preact
320 */
321export interface Options {
322 /** Attach a hook that is invoked whenever a VNode is created. */
323 vnode?(vnode: VNode): void;
324 /** Attach a hook that is invoked immediately before a vnode is unmounted. */
325 unmount?(vnode: VNode): void;
326 /** Attach a hook that is invoked after a vnode has rendered. */
327 diffed?(vnode: VNode): void;
328 event?(e: Event): any;
329 requestAnimationFrame?(callback: () => void): void;
330 debounceRendering?(cb: () => void): void;
331 useDebugValue?(value: string | number): void;
332 _addHookName?(name: string | number): void;
333 __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
334 // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
335}
336
337export const options: Options;
338
339//
340// Preact helpers
341// -----------------------------------
342export function createRef<T = any>(): RefObject<T>;
343export function toChildArray(
344 children: ComponentChildren
345): Array<VNode | string | number>;
346export function isValidElement(vnode: any): vnode is VNode;
347
348//
349// Context
350// -----------------------------------
351export interface Consumer<T>
352 extends FunctionComponent<{
353 children: (value: T) => ComponentChildren;
354 }> {}
355export interface PreactConsumer<T> extends Consumer<T> {}
356
357export interface Provider<T>
358 extends FunctionComponent<{
359 value: T;
360 children: ComponentChildren;
361 }> {}
362export interface PreactProvider<T> extends Provider<T> {}
363export type ContextType<C extends Context<any>> = C extends Context<infer T>
364 ? T
365 : never;
366
367export interface Context<T> {
368 Consumer: Consumer<T>;
369 Provider: Provider<T>;
370 displayName?: string;
371}
372export interface PreactContext<T> extends Context<T> {}
373
374export function createContext<T>(defaultValue: T): Context<T>;