UNPKG

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