UNPKG

8.35 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>;
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 type RenderableProps<P, RefType = any> = P &
72 Readonly<Attributes & { children?: ComponentChildren; ref?: Ref<RefType> }>;
73
74export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
75export type ComponentFactory<P = {}> = ComponentType<P>;
76
77export type ComponentProps<
78 C extends ComponentType<any> | keyof JSXInternal.IntrinsicElements
79> = C extends ComponentType<infer P>
80 ? P
81 : C extends keyof JSXInternal.IntrinsicElements
82 ? JSXInternal.IntrinsicElements[C]
83 : never;
84
85export interface FunctionComponent<P = {}> {
86 (props: RenderableProps<P>, context?: any): VNode<any> | null;
87 displayName?: string;
88 defaultProps?: Partial<P>;
89}
90export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
91
92export interface ComponentClass<P = {}, S = {}> {
93 new (props: P, context?: any): Component<P, S>;
94 displayName?: string;
95 defaultProps?: Partial<P>;
96 contextType?: Context<any>;
97 getDerivedStateFromProps?(
98 props: Readonly<P>,
99 state: Readonly<S>
100 ): Partial<S> | null;
101 getDerivedStateFromError?(error: any): Partial<S> | null;
102}
103export interface ComponentConstructor<P = {}, S = {}>
104 extends ComponentClass<P, S> {}
105
106// Type alias for a component instance considered generally, whether stateless or stateful.
107export type AnyComponent<P = {}, S = {}> =
108 | FunctionComponent<P>
109 | Component<P, S>;
110
111export interface Component<P = {}, S = {}> {
112 componentWillMount?(): void;
113 componentDidMount?(): void;
114 componentWillUnmount?(): void;
115 getChildContext?(): object;
116 componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
117 shouldComponentUpdate?(
118 nextProps: Readonly<P>,
119 nextState: Readonly<S>,
120 nextContext: any
121 ): boolean;
122 componentWillUpdate?(
123 nextProps: Readonly<P>,
124 nextState: Readonly<S>,
125 nextContext: any
126 ): void;
127 getSnapshotBeforeUpdate?(oldProps: Readonly<P>, oldState: Readonly<S>): any;
128 componentDidUpdate?(
129 previousProps: Readonly<P>,
130 previousState: Readonly<S>,
131 snapshot: any
132 ): void;
133 componentDidCatch?(error: any, errorInfo: any): void;
134}
135
136export abstract class Component<P, S> {
137 constructor(props?: P, context?: any);
138
139 static displayName?: string;
140 static defaultProps?: any;
141 static contextType?: Context<any>;
142
143 // Static members cannot reference class type parameters. This is not
144 // supported in TypeScript. Reusing the same type arguments from `Component`
145 // will lead to an impossible state where one cannot satisfy the type
146 // constraint under no circumstances, see #1356.In general type arguments
147 // seem to be a bit buggy and not supported well at the time of this
148 // writing with TS 3.3.3333.
149 static getDerivedStateFromProps?(
150 props: Readonly<object>,
151 state: Readonly<object>
152 ): object | null;
153 static getDerivedStateFromError?(error: any): object | null;
154
155 state: Readonly<S>;
156 props: RenderableProps<P>;
157 context: any;
158 base?: Element | Text;
159
160 // From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e836acc75a78cf0655b5dfdbe81d69fdd4d8a252/types/react/index.d.ts#L402
161 // // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
162 // // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
163 setState<K extends keyof S>(
164 state:
165 | ((
166 prevState: Readonly<S>,
167 props: Readonly<P>
168 ) => Pick<S, K> | Partial<S> | null)
169 | (Pick<S, K> | Partial<S> | null),
170 callback?: () => void
171 ): void;
172
173 forceUpdate(callback?: () => void): void;
174
175 abstract render(
176 props?: RenderableProps<P>,
177 state?: Readonly<S>,
178 context?: any
179 ): ComponentChild;
180}
181
182//
183// Preact createElement
184// -----------------------------------
185
186export function createElement(
187 type: string,
188 props:
189 | (JSXInternal.HTMLAttributes &
190 JSXInternal.SVGAttributes &
191 Record<string, any>)
192 | null,
193 ...children: ComponentChildren[]
194): VNode<any>;
195export function createElement<P>(
196 type: ComponentType<P>,
197 props: (Attributes & P) | null,
198 ...children: ComponentChildren[]
199): VNode<any>;
200export namespace createElement {
201 export import JSX = JSXInternal;
202}
203
204export function h(
205 type: string,
206 props:
207 | (JSXInternal.HTMLAttributes &
208 JSXInternal.SVGAttributes &
209 Record<string, any>)
210 | null,
211 ...children: ComponentChildren[]
212): VNode<any>;
213export function h<P>(
214 type: ComponentType<P>,
215 props: (Attributes & P) | null,
216 ...children: ComponentChildren[]
217): VNode<any>;
218export namespace h {
219 export import JSX = JSXInternal;
220}
221
222//
223// Preact render
224// -----------------------------------
225
226export function render(
227 vnode: ComponentChild,
228 parent: Element | Document | ShadowRoot | DocumentFragment,
229 replaceNode?: Element | Text
230): void;
231export function hydrate(
232 vnode: ComponentChild,
233 parent: Element | Document | ShadowRoot | DocumentFragment
234): void;
235export function cloneElement(
236 vnode: VNode<any>,
237 props?: any,
238 ...children: ComponentChildren[]
239): VNode<any>;
240export function cloneElement<P>(
241 vnode: VNode<P>,
242 props?: any,
243 ...children: ComponentChildren[]
244): VNode<P>;
245
246//
247// Preact Built-in Components
248// -----------------------------------
249
250// TODO: Revisit what the public type of this is...
251export const Fragment: ComponentClass<{}, {}>;
252
253//
254// Preact options
255// -----------------------------------
256
257/**
258 * Global options for preact
259 */
260export interface Options {
261 /** Attach a hook that is invoked whenever a VNode is created. */
262 vnode?(vnode: VNode): void;
263 /** Attach a hook that is invoked immediately before a vnode is unmounted. */
264 unmount?(vnode: VNode): void;
265 /** Attach a hook that is invoked after a vnode has rendered. */
266 diffed?(vnode: VNode): void;
267 event?(e: Event): any;
268 requestAnimationFrame?(callback: () => void): void;
269 debounceRendering?(cb: () => void): void;
270 useDebugValue?(value: string | number): void;
271 _addHookName?(name: string | number): void;
272 __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
273 // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
274}
275
276export const options: Options;
277
278//
279// Preact helpers
280// -----------------------------------
281export function createRef<T = any>(): RefObject<T>;
282export function toChildArray(
283 children: ComponentChildren
284): Array<VNode | string | number>;
285export function isValidElement(vnode: any): vnode is VNode;
286
287//
288// Context
289// -----------------------------------
290export interface Consumer<T>
291 extends FunctionComponent<{
292 children: (value: T) => ComponentChildren;
293 }> {}
294export interface PreactConsumer<T> extends Consumer<T> {}
295
296export interface Provider<T>
297 extends FunctionComponent<{
298 value: T;
299 children: ComponentChildren;
300 }> {}
301export interface PreactProvider<T> extends Provider<T> {}
302
303export interface Context<T> {
304 Consumer: Consumer<T>;
305 Provider: Provider<T>;
306 displayName?: string;
307}
308export interface PreactContext<T> extends Context<T> {}
309
310export function createContext<T>(defaultValue: T): Context<T>;