UNPKG

194 kBTypeScriptView Raw
1// NOTE: Users of the `experimental` builds of React should add a reference
2// to 'react/experimental' in their project. See experimental.d.ts's top comment
3// for reference and documentation on how exactly to do it.
4
5/// <reference path="global.d.ts" />
6
7import * as CSS from "csstype";
8import * as PropTypes from "prop-types";
9
10type NativeAnimationEvent = AnimationEvent;
11type NativeClipboardEvent = ClipboardEvent;
12type NativeCompositionEvent = CompositionEvent;
13type NativeDragEvent = DragEvent;
14type NativeFocusEvent = FocusEvent;
15type NativeKeyboardEvent = KeyboardEvent;
16type NativeMouseEvent = MouseEvent;
17type NativeTouchEvent = TouchEvent;
18type NativePointerEvent = PointerEvent;
19type NativeTransitionEvent = TransitionEvent;
20type NativeUIEvent = UIEvent;
21type NativeWheelEvent = WheelEvent;
22
23/**
24 * Used to represent DOM API's where users can either pass
25 * true or false as a boolean or as its equivalent strings.
26 */
27type Booleanish = boolean | "true" | "false";
28
29/**
30 * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin MDN}
31 */
32type CrossOrigin = "anonymous" | "use-credentials" | "" | undefined;
33
34declare const UNDEFINED_VOID_ONLY: unique symbol;
35
36/**
37 * The function returned from an effect passed to {@link React.useEffect useEffect},
38 * which can be used to clean up the effect when the component unmounts.
39 *
40 * @see {@link https://react.dev/reference/react/useEffect React Docs}
41 */
42type Destructor = () => void | { [UNDEFINED_VOID_ONLY]: never };
43type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
44
45// eslint-disable-next-line @definitelytyped/export-just-namespace
46export = React;
47export as namespace React;
48
49declare namespace React {
50 //
51 // React Elements
52 // ----------------------------------------------------------------------
53
54 /**
55 * Used to retrieve the possible components which accept a given set of props.
56 *
57 * Can be passed no type parameters to get a union of all possible components
58 * and tags.
59 *
60 * Is a superset of {@link ComponentType}.
61 *
62 * @template P The props to match against. If not passed, defaults to any.
63 * @template Tag An optional tag to match against. If not passed, attempts to match against all possible tags.
64 *
65 * @example
66 *
67 * ```tsx
68 * // All components and tags (img, embed etc.)
69 * // which accept `src`
70 * type SrcComponents = ElementType<{ src: any }>;
71 * ```
72 *
73 * @example
74 *
75 * ```tsx
76 * // All components
77 * type AllComponents = ElementType;
78 * ```
79 *
80 * @example
81 *
82 * ```tsx
83 * // All custom components which match `src`, and tags which
84 * // match `src`, narrowed down to just `audio` and `embed`
85 * type SrcComponents = ElementType<{ src: any }, 'audio' | 'embed'>;
86 * ```
87 */
88 type ElementType<P = any, Tag extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements> =
89 | { [K in Tag]: P extends JSX.IntrinsicElements[K] ? K : never }[Tag]
90 | ComponentType<P>;
91
92 /**
93 * Represents any user-defined component, either as a function or a class.
94 *
95 * Similar to {@link JSXElementConstructor}, but with extra properties like
96 * {@link FunctionComponent.defaultProps defaultProps } and
97 * {@link ComponentClass.contextTypes contextTypes}.
98 *
99 * @template P The props the component accepts.
100 *
101 * @see {@link ComponentClass}
102 * @see {@link FunctionComponent}
103 */
104 type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
105
106 /**
107 * Represents any user-defined component, either as a function or a class.
108 *
109 * Similar to {@link ComponentType}, but without extra properties like
110 * {@link FunctionComponent.defaultProps defaultProps } and
111 * {@link ComponentClass.contextTypes contextTypes}.
112 *
113 * @template P The props the component accepts.
114 */
115 type JSXElementConstructor<P> =
116 | ((
117 props: P,
118 /**
119 * @deprecated
120 *
121 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-stateless-function-components React Docs}
122 */
123 deprecatedLegacyContext?: any,
124 ) => ReactNode)
125 | (new(
126 props: P,
127 /**
128 * @deprecated
129 *
130 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods React Docs}
131 */
132 deprecatedLegacyContext?: any,
133 ) => Component<any, any>);
134
135 /**
136 * A readonly ref container where {@link current} cannot be mutated.
137 *
138 * Created by {@link createRef}, or {@link useRef} when passed `null`.
139 *
140 * @template T The type of the ref's value.
141 *
142 * @example
143 *
144 * ```tsx
145 * const ref = createRef<HTMLDivElement>();
146 *
147 * ref.current = document.createElement('div'); // Error
148 * ```
149 */
150 interface RefObject<T> {
151 /**
152 * The current value of the ref.
153 */
154 readonly current: T | null;
155 }
156
157 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES {
158 }
159 /**
160 * A callback fired whenever the ref's value changes.
161 *
162 * @template T The type of the ref's value.
163 *
164 * @see {@link https://react.dev/reference/react-dom/components/common#ref-callback React Docs}
165 *
166 * @example
167 *
168 * ```tsx
169 * <div ref={(node) => console.log(node)} />
170 * ```
171 */
172 type RefCallback<T> = {
173 bivarianceHack(
174 instance: T | null,
175 ):
176 | void
177 | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[
178 keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES
179 ];
180 }["bivarianceHack"];
181
182 /**
183 * A union type of all possible shapes for React refs.
184 *
185 * @see {@link RefCallback}
186 * @see {@link RefObject}
187 */
188
189 type Ref<T> = RefCallback<T> | RefObject<T> | null;
190 /**
191 * A legacy implementation of refs where you can pass a string to a ref prop.
192 *
193 * @see {@link https://react.dev/reference/react/Component#refs React Docs}
194 *
195 * @example
196 *
197 * ```tsx
198 * <div ref="myRef" />
199 * ```
200 */
201 // TODO: Remove the string ref special case from `PropsWithRef` once we remove LegacyRef
202 type LegacyRef<T> = string | Ref<T>;
203
204 /**
205 * Retrieves the type of the 'ref' prop for a given component type or tag name.
206 *
207 * @template C The component type.
208 *
209 * @example
210 *
211 * ```tsx
212 * type MyComponentRef = React.ElementRef<typeof MyComponent>;
213 * ```
214 *
215 * @example
216 *
217 * ```tsx
218 * type DivRef = React.ElementRef<'div'>;
219 * ```
220 */
221 type ElementRef<
222 C extends
223 | ForwardRefExoticComponent<any>
224 | { new(props: any): Component<any> }
225 | ((props: any, deprecatedLegacyContext?: any) => ReactNode)
226 | keyof JSX.IntrinsicElements,
227 > =
228 // need to check first if `ref` is a valid prop for ts@3.0
229 // otherwise it will infer `{}` instead of `never`
230 "ref" extends keyof ComponentPropsWithRef<C>
231 ? NonNullable<ComponentPropsWithRef<C>["ref"]> extends RefAttributes<
232 infer Instance
233 >["ref"] ? Instance
234 : never
235 : never;
236
237 type ComponentState = any;
238
239 /**
240 * A value which uniquely identifies a node among items in an array.
241 *
242 * @see {@link https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key React Docs}
243 */
244 type Key = string | number | bigint;
245
246 /**
247 * @internal The props any component can receive.
248 * You don't have to add this type. All components automatically accept these props.
249 * ```tsx
250 * const Component = () => <div />;
251 * <Component key="one" />
252 * ```
253 *
254 * WARNING: The implementation of a component will never have access to these attributes.
255 * The following example would be incorrect usage because {@link Component} would never have access to `key`:
256 * ```tsx
257 * const Component = (props: React.Attributes) => props.key;
258 * ```
259 */
260 interface Attributes {
261 key?: Key | null | undefined;
262 }
263 /**
264 * The props any component accepting refs can receive.
265 * Class components, built-in browser components (e.g. `div`) and forwardRef components can receive refs and automatically accept these props.
266 * ```tsx
267 * const Component = forwardRef(() => <div />);
268 * <Component ref={(current) => console.log(current)} />
269 * ```
270 *
271 * You only need this type if you manually author the types of props that need to be compatible with legacy refs.
272 * ```tsx
273 * interface Props extends React.RefAttributes<HTMLDivElement> {}
274 * declare const Component: React.FunctionComponent<Props>;
275 * ```
276 *
277 * Otherwise it's simpler to directly use {@link Ref} since you can safely use the
278 * props type to describe to props that a consumer can pass to the component
279 * as well as describing the props the implementation of a component "sees".
280 * {@link RefAttributes} is generally not safe to describe both consumer and seen props.
281 *
282 * ```tsx
283 * interface Props extends {
284 * ref?: React.Ref<HTMLDivElement> | undefined;
285 * }
286 * declare const Component: React.FunctionComponent<Props>;
287 * ```
288 *
289 * WARNING: The implementation of a component will not have access to the same type in versions of React supporting string refs.
290 * The following example would be incorrect usage because {@link Component} would never have access to a `ref` with type `string`
291 * ```tsx
292 * const Component = (props: React.RefAttributes) => props.ref;
293 * ```
294 */
295 interface RefAttributes<T> extends Attributes {
296 /**
297 * Allows getting a ref to the component instance.
298 * Once the component unmounts, React will set `ref.current` to `null`
299 * (or call the ref with `null` if you passed a callback ref).
300 *
301 * @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
302 */
303 ref?: LegacyRef<T> | undefined;
304 }
305
306 /**
307 * Represents the built-in attributes available to class components.
308 */
309 interface ClassAttributes<T> extends RefAttributes<T> {
310 }
311
312 /**
313 * Represents a JSX element.
314 *
315 * Where {@link ReactNode} represents everything that can be rendered, `ReactElement`
316 * only represents JSX.
317 *
318 * @template P The type of the props object
319 * @template T The type of the component or tag
320 *
321 * @example
322 *
323 * ```tsx
324 * const element: ReactElement = <div />;
325 * ```
326 */
327 interface ReactElement<
328 P = any,
329 T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>,
330 > {
331 type: T;
332 props: P;
333 key: string | null;
334 }
335
336 /**
337 * @deprecated
338 */
339 interface ReactComponentElement<
340 T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
341 P = Pick<ComponentProps<T>, Exclude<keyof ComponentProps<T>, "key" | "ref">>,
342 > extends ReactElement<P, Exclude<T, number>> {}
343
344 interface FunctionComponentElement<P> extends ReactElement<P, FunctionComponent<P>> {
345 ref?: ("ref" extends keyof P ? P extends { ref?: infer R | undefined } ? R : never : never) | undefined;
346 }
347
348 type CElement<P, T extends Component<P, ComponentState>> = ComponentElement<P, T>;
349 interface ComponentElement<P, T extends Component<P, ComponentState>> extends ReactElement<P, ComponentClass<P>> {
350 ref?: LegacyRef<T> | undefined;
351 }
352
353 /**
354 * @deprecated Use {@link ComponentElement} instead.
355 */
356 type ClassicElement<P> = CElement<P, ClassicComponent<P, ComponentState>>;
357
358 // string fallback for custom web-components
359 interface DOMElement<P extends HTMLAttributes<T> | SVGAttributes<T>, T extends Element>
360 extends ReactElement<P, string>
361 {
362 ref: LegacyRef<T>;
363 }
364
365 // ReactHTML for ReactHTMLElement
366 interface ReactHTMLElement<T extends HTMLElement> extends DetailedReactHTMLElement<AllHTMLAttributes<T>, T> {}
367
368 interface DetailedReactHTMLElement<P extends HTMLAttributes<T>, T extends HTMLElement> extends DOMElement<P, T> {
369 type: keyof ReactHTML;
370 }
371
372 // ReactSVG for ReactSVGElement
373 interface ReactSVGElement extends DOMElement<SVGAttributes<SVGElement>, SVGElement> {
374 type: keyof ReactSVG;
375 }
376
377 interface ReactPortal extends ReactElement {
378 children: ReactNode;
379 }
380
381 //
382 // Factories
383 // ----------------------------------------------------------------------
384
385 type Factory<P> = (props?: Attributes & P, ...children: ReactNode[]) => ReactElement<P>;
386
387 /**
388 * @deprecated Please use `FunctionComponentFactory`
389 */
390 type SFCFactory<P> = FunctionComponentFactory<P>;
391
392 type FunctionComponentFactory<P> = (
393 props?: Attributes & P,
394 ...children: ReactNode[]
395 ) => FunctionComponentElement<P>;
396
397 type ComponentFactory<P, T extends Component<P, ComponentState>> = (
398 props?: ClassAttributes<T> & P,
399 ...children: ReactNode[]
400 ) => CElement<P, T>;
401
402 type CFactory<P, T extends Component<P, ComponentState>> = ComponentFactory<P, T>;
403 type ClassicFactory<P> = CFactory<P, ClassicComponent<P, ComponentState>>;
404
405 type DOMFactory<P extends DOMAttributes<T>, T extends Element> = (
406 props?: ClassAttributes<T> & P | null,
407 ...children: ReactNode[]
408 ) => DOMElement<P, T>;
409
410 interface HTMLFactory<T extends HTMLElement> extends DetailedHTMLFactory<AllHTMLAttributes<T>, T> {}
411
412 interface DetailedHTMLFactory<P extends HTMLAttributes<T>, T extends HTMLElement> extends DOMFactory<P, T> {
413 (props?: ClassAttributes<T> & P | null, ...children: ReactNode[]): DetailedReactHTMLElement<P, T>;
414 }
415
416 interface SVGFactory extends DOMFactory<SVGAttributes<SVGElement>, SVGElement> {
417 (
418 props?: ClassAttributes<SVGElement> & SVGAttributes<SVGElement> | null,
419 ...children: ReactNode[]
420 ): ReactSVGElement;
421 }
422
423 /**
424 * @deprecated - This type is not relevant when using React. Inline the type instead to make the intent clear.
425 */
426 type ReactText = string | number;
427 /**
428 * @deprecated - This type is not relevant when using React. Inline the type instead to make the intent clear.
429 */
430 type ReactChild = ReactElement | string | number;
431
432 /**
433 * @deprecated Use either `ReactNode[]` if you need an array or `Iterable<ReactNode>` if its passed to a host component.
434 */
435 interface ReactNodeArray extends ReadonlyArray<ReactNode> {}
436 /**
437 * WARNING: Not related to `React.Fragment`.
438 * @deprecated This type is not relevant when using React. Inline the type instead to make the intent clear.
439 */
440 type ReactFragment = Iterable<ReactNode>;
441
442 /**
443 * Different release channels declare additional types of ReactNode this particular release channel accepts.
444 * App or library types should never augment this interface.
445 */
446 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES {}
447
448 /**
449 * Represents all of the things React can render.
450 *
451 * Where {@link ReactElement} only represents JSX, `ReactNode` represents everything that can be rendered.
452 *
453 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/reactnode/ React TypeScript Cheatsheet}
454 *
455 * @example
456 *
457 * ```tsx
458 * // Typing children
459 * type Props = { children: ReactNode }
460 *
461 * const Component = ({ children }: Props) => <div>{children}</div>
462 *
463 * <Component>hello</Component>
464 * ```
465 *
466 * @example
467 *
468 * ```tsx
469 * // Typing a custom element
470 * type Props = { customElement: ReactNode }
471 *
472 * const Component = ({ customElement }: Props) => <div>{customElement}</div>
473 *
474 * <Component customElement={<div>hello</div>} />
475 * ```
476 */
477 // non-thenables need to be kept in sync with AwaitedReactNode
478 type ReactNode =
479 | ReactElement
480 | string
481 | number
482 | Iterable<ReactNode>
483 | ReactPortal
484 | boolean
485 | null
486 | undefined
487 | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
488 keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
489 ];
490
491 //
492 // Top Level API
493 // ----------------------------------------------------------------------
494
495 // DOM Elements
496 /** @deprecated */
497 function createFactory<T extends HTMLElement>(
498 type: keyof ReactHTML,
499 ): HTMLFactory<T>;
500 /** @deprecated */
501 function createFactory(
502 type: keyof ReactSVG,
503 ): SVGFactory;
504 /** @deprecated */
505 function createFactory<P extends DOMAttributes<T>, T extends Element>(
506 type: string,
507 ): DOMFactory<P, T>;
508
509 // Custom components
510 /** @deprecated */
511 function createFactory<P>(type: FunctionComponent<P>): FunctionComponentFactory<P>;
512 /** @deprecated */
513 function createFactory<P, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(
514 type: ClassType<P, T, C>,
515 ): CFactory<P, T>;
516 /** @deprecated */
517 function createFactory<P>(type: ComponentClass<P>): Factory<P>;
518
519 // DOM Elements
520 // TODO: generalize this to everything in `keyof ReactHTML`, not just "input"
521 function createElement(
522 type: "input",
523 props?: InputHTMLAttributes<HTMLInputElement> & ClassAttributes<HTMLInputElement> | null,
524 ...children: ReactNode[]
525 ): DetailedReactHTMLElement<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>;
526 function createElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
527 type: keyof ReactHTML,
528 props?: ClassAttributes<T> & P | null,
529 ...children: ReactNode[]
530 ): DetailedReactHTMLElement<P, T>;
531 function createElement<P extends SVGAttributes<T>, T extends SVGElement>(
532 type: keyof ReactSVG,
533 props?: ClassAttributes<T> & P | null,
534 ...children: ReactNode[]
535 ): ReactSVGElement;
536 function createElement<P extends DOMAttributes<T>, T extends Element>(
537 type: string,
538 props?: ClassAttributes<T> & P | null,
539 ...children: ReactNode[]
540 ): DOMElement<P, T>;
541
542 // Custom components
543
544 function createElement<P extends {}>(
545 type: FunctionComponent<P>,
546 props?: Attributes & P | null,
547 ...children: ReactNode[]
548 ): FunctionComponentElement<P>;
549 function createElement<P extends {}, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(
550 type: ClassType<P, T, C>,
551 props?: ClassAttributes<T> & P | null,
552 ...children: ReactNode[]
553 ): CElement<P, T>;
554 function createElement<P extends {}>(
555 type: FunctionComponent<P> | ComponentClass<P> | string,
556 props?: Attributes & P | null,
557 ...children: ReactNode[]
558 ): ReactElement<P>;
559
560 // DOM Elements
561 // ReactHTMLElement
562 function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
563 element: DetailedReactHTMLElement<P, T>,
564 props?: P,
565 ...children: ReactNode[]
566 ): DetailedReactHTMLElement<P, T>;
567 // ReactHTMLElement, less specific
568 function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
569 element: ReactHTMLElement<T>,
570 props?: P,
571 ...children: ReactNode[]
572 ): ReactHTMLElement<T>;
573 // SVGElement
574 function cloneElement<P extends SVGAttributes<T>, T extends SVGElement>(
575 element: ReactSVGElement,
576 props?: P,
577 ...children: ReactNode[]
578 ): ReactSVGElement;
579 // DOM Element (has to be the last, because type checking stops at first overload that fits)
580 function cloneElement<P extends DOMAttributes<T>, T extends Element>(
581 element: DOMElement<P, T>,
582 props?: DOMAttributes<T> & P,
583 ...children: ReactNode[]
584 ): DOMElement<P, T>;
585
586 // Custom components
587 function cloneElement<P>(
588 element: FunctionComponentElement<P>,
589 props?: Partial<P> & Attributes,
590 ...children: ReactNode[]
591 ): FunctionComponentElement<P>;
592 function cloneElement<P, T extends Component<P, ComponentState>>(
593 element: CElement<P, T>,
594 props?: Partial<P> & ClassAttributes<T>,
595 ...children: ReactNode[]
596 ): CElement<P, T>;
597 function cloneElement<P>(
598 element: ReactElement<P>,
599 props?: Partial<P> & Attributes,
600 ...children: ReactNode[]
601 ): ReactElement<P>;
602
603 /**
604 * Describes the props accepted by a Context {@link Provider}.
605 *
606 * @template T The type of the value the context provides.
607 */
608 interface ProviderProps<T> {
609 value: T;
610 children?: ReactNode | undefined;
611 }
612
613 /**
614 * Describes the props accepted by a Context {@link Consumer}.
615 *
616 * @template T The type of the value the context provides.
617 */
618 interface ConsumerProps<T> {
619 children: (value: T) => ReactNode;
620 }
621
622 /**
623 * An object masquerading as a component. These are created by functions
624 * like {@link forwardRef}, {@link memo}, and {@link createContext}.
625 *
626 * In order to make TypeScript work, we pretend that they are normal
627 * components.
628 *
629 * But they are, in fact, not callable - instead, they are objects which
630 * are treated specially by the renderer.
631 *
632 * @template P The props the component accepts.
633 */
634 interface ExoticComponent<P = {}> {
635 (props: P): ReactNode;
636 readonly $$typeof: symbol;
637 }
638
639 /**
640 * An {@link ExoticComponent} with a `displayName` property applied to it.
641 *
642 * @template P The props the component accepts.
643 */
644 interface NamedExoticComponent<P = {}> extends ExoticComponent<P> {
645 /**
646 * Used in debugging messages. You might want to set it
647 * explicitly if you want to display a different name for
648 * debugging purposes.
649 *
650 * @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
651 */
652 displayName?: string | undefined;
653 }
654
655 /**
656 * An {@link ExoticComponent} with a `propTypes` property applied to it.
657 *
658 * @template P The props the component accepts.
659 */
660 interface ProviderExoticComponent<P> extends ExoticComponent<P> {
661 propTypes?: WeakValidationMap<P> | undefined;
662 }
663
664 /**
665 * Used to retrieve the type of a context object from a {@link Context}.
666 *
667 * @template C The context object.
668 *
669 * @example
670 *
671 * ```tsx
672 * import { createContext } from 'react';
673 *
674 * const MyContext = createContext({ foo: 'bar' });
675 *
676 * type ContextType = ContextType<typeof MyContext>;
677 * // ContextType = { foo: string }
678 * ```
679 */
680 type ContextType<C extends Context<any>> = C extends Context<infer T> ? T : never;
681
682 /**
683 * Wraps your components to specify the value of this context for all components inside.
684 *
685 * @see {@link https://react.dev/reference/react/createContext#provider React Docs}
686 *
687 * @example
688 *
689 * ```tsx
690 * import { createContext } from 'react';
691 *
692 * const ThemeContext = createContext('light');
693 *
694 * function App() {
695 * return (
696 * <ThemeContext.Provider value="dark">
697 * <Toolbar />
698 * </ThemeContext.Provider>
699 * );
700 * }
701 * ```
702 */
703 type Provider<T> = ProviderExoticComponent<ProviderProps<T>>;
704
705 /**
706 * The old way to read context, before {@link useContext} existed.
707 *
708 * @see {@link https://react.dev/reference/react/createContext#consumer React Docs}
709 *
710 * @example
711 *
712 * ```tsx
713 * import { UserContext } from './user-context';
714 *
715 * function Avatar() {
716 * return (
717 * <UserContext.Consumer>
718 * {user => <img src={user.profileImage} alt={user.name} />}
719 * </UserContext.Consumer>
720 * );
721 * }
722 * ```
723 */
724 type Consumer<T> = ExoticComponent<ConsumerProps<T>>;
725
726 /**
727 * Context lets components pass information deep down without explicitly
728 * passing props.
729 *
730 * Created from {@link createContext}
731 *
732 * @see {@link https://react.dev/learn/passing-data-deeply-with-context React Docs}
733 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/context/ React TypeScript Cheatsheet}
734 *
735 * @example
736 *
737 * ```tsx
738 * import { createContext } from 'react';
739 *
740 * const ThemeContext = createContext('light');
741 * ```
742 */
743 interface Context<T> {
744 Provider: Provider<T>;
745 Consumer: Consumer<T>;
746 /**
747 * Used in debugging messages. You might want to set it
748 * explicitly if you want to display a different name for
749 * debugging purposes.
750 *
751 * @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
752 */
753 displayName?: string | undefined;
754 }
755
756 /**
757 * Lets you create a {@link Context} that components can provide or read.
758 *
759 * @param defaultValue The value you want the context to have when there is no matching
760 * {@link Provider} in the tree above the component reading the context. This is meant
761 * as a "last resort" fallback.
762 *
763 * @see {@link https://react.dev/reference/react/createContext#reference React Docs}
764 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/context/ React TypeScript Cheatsheet}
765 *
766 * @example
767 *
768 * ```tsx
769 * import { createContext } from 'react';
770 *
771 * const ThemeContext = createContext('light');
772 * ```
773 */
774 function createContext<T>(
775 // If you thought this should be optional, see
776 // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/24509#issuecomment-382213106
777 defaultValue: T,
778 ): Context<T>;
779
780 function isValidElement<P>(object: {} | null | undefined): object is ReactElement<P>;
781
782 /**
783 * Maintainer's note: Sync with {@link ReactChildren} until {@link ReactChildren} is removed.
784 */
785 const Children: {
786 map<T, C>(
787 children: C | readonly C[],
788 fn: (child: C, index: number) => T,
789 ): C extends null | undefined ? C : Array<Exclude<T, boolean | null | undefined>>;
790 forEach<C>(children: C | readonly C[], fn: (child: C, index: number) => void): void;
791 count(children: any): number;
792 only<C>(children: C): C extends any[] ? never : C;
793 toArray(children: ReactNode | ReactNode[]): Array<Exclude<ReactNode, boolean | null | undefined>>;
794 };
795 /**
796 * Lets you group elements without a wrapper node.
797 *
798 * @see {@link https://react.dev/reference/react/Fragment React Docs}
799 *
800 * @example
801 *
802 * ```tsx
803 * import { Fragment } from 'react';
804 *
805 * <Fragment>
806 * <td>Hello</td>
807 * <td>World</td>
808 * </Fragment>
809 * ```
810 *
811 * @example
812 *
813 * ```tsx
814 * // Using the <></> shorthand syntax:
815 *
816 * <>
817 * <td>Hello</td>
818 * <td>World</td>
819 * </>
820 * ```
821 */
822 const Fragment: ExoticComponent<{ children?: ReactNode | undefined }>;
823
824 /**
825 * Lets you find common bugs in your components early during development.
826 *
827 * @see {@link https://react.dev/reference/react/StrictMode React Docs}
828 *
829 * @example
830 *
831 * ```tsx
832 * import { StrictMode } from 'react';
833 *
834 * <StrictMode>
835 * <App />
836 * </StrictMode>
837 * ```
838 */
839 const StrictMode: ExoticComponent<{ children?: ReactNode | undefined }>;
840
841 /**
842 * The props accepted by {@link Suspense}.
843 *
844 * @see {@link https://react.dev/reference/react/Suspense React Docs}
845 */
846 interface SuspenseProps {
847 children?: ReactNode | undefined;
848
849 /** A fallback react tree to show when a Suspense child (like React.lazy) suspends */
850 fallback?: ReactNode;
851
852 /**
853 * A name for this Suspense boundary for instrumentation purposes.
854 * The name will help identify this boundary in React DevTools.
855 */
856 name?: string | undefined;
857 }
858
859 /**
860 * Lets you display a fallback until its children have finished loading.
861 *
862 * @see {@link https://react.dev/reference/react/Suspense React Docs}
863 *
864 * @example
865 *
866 * ```tsx
867 * import { Suspense } from 'react';
868 *
869 * <Suspense fallback={<Loading />}>
870 * <ProfileDetails />
871 * </Suspense>
872 * ```
873 */
874 const Suspense: ExoticComponent<SuspenseProps>;
875 const version: string;
876
877 /**
878 * The callback passed to {@link ProfilerProps.onRender}.
879 *
880 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
881 */
882 type ProfilerOnRenderCallback = (
883 /**
884 * The string id prop of the {@link Profiler} tree that has just committed. This lets
885 * you identify which part of the tree was committed if you are using multiple
886 * profilers.
887 *
888 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
889 */
890 id: string,
891 /**
892 * This lets you know whether the tree has just been mounted for the first time
893 * or re-rendered due to a change in props, state, or hooks.
894 *
895 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
896 */
897 phase: "mount" | "update" | "nested-update",
898 /**
899 * The number of milliseconds spent rendering the {@link Profiler} and its descendants
900 * for the current update. This indicates how well the subtree makes use of
901 * memoization (e.g. {@link memo} and {@link useMemo}). Ideally this value should decrease
902 * significantly after the initial mount as many of the descendants will only need to
903 * re-render if their specific props change.
904 *
905 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
906 */
907 actualDuration: number,
908 /**
909 * The number of milliseconds estimating how much time it would take to re-render the entire
910 * {@link Profiler} subtree without any optimizations. It is calculated by summing up the most
911 * recent render durations of each component in the tree. This value estimates a worst-case
912 * cost of rendering (e.g. the initial mount or a tree with no memoization). Compare
913 * {@link actualDuration} against it to see if memoization is working.
914 *
915 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
916 */
917 baseDuration: number,
918 /**
919 * A numeric timestamp for when React began rendering the current update.
920 *
921 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
922 */
923 startTime: number,
924 /**
925 * A numeric timestamp for when React committed the current update. This value is shared
926 * between all profilers in a commit, enabling them to be grouped if desirable.
927 *
928 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
929 */
930 commitTime: number,
931 ) => void;
932
933 /**
934 * The props accepted by {@link Profiler}.
935 *
936 * @see {@link https://react.dev/reference/react/Profiler React Docs}
937 */
938 interface ProfilerProps {
939 children?: ReactNode | undefined;
940 id: string;
941 onRender: ProfilerOnRenderCallback;
942 }
943
944 /**
945 * Lets you measure rendering performance of a React tree programmatically.
946 *
947 * @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
948 *
949 * @example
950 *
951 * ```tsx
952 * <Profiler id="App" onRender={onRender}>
953 * <App />
954 * </Profiler>
955 * ```
956 */
957 const Profiler: ExoticComponent<ProfilerProps>;
958
959 //
960 // Component API
961 // ----------------------------------------------------------------------
962
963 type ReactInstance = Component<any> | Element;
964
965 // Base component for plain JS classes
966 interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {}
967 class Component<P, S> {
968 /**
969 * If set, `this.context` will be set at runtime to the current value of the given Context.
970 *
971 * @example
972 *
973 * ```ts
974 * type MyContext = number
975 * const Ctx = React.createContext<MyContext>(0)
976 *
977 * class Foo extends React.Component {
978 * static contextType = Ctx
979 * context!: React.ContextType<typeof Ctx>
980 * render () {
981 * return <>My context's value: {this.context}</>;
982 * }
983 * }
984 * ```
985 *
986 * @see {@link https://react.dev/reference/react/Component#static-contexttype}
987 */
988 static contextType?: Context<any> | undefined;
989
990 /**
991 * If using the new style context, re-declare this in your class to be the
992 * `React.ContextType` of your `static contextType`.
993 * Should be used with type annotation or static contextType.
994 *
995 * @example
996 * ```ts
997 * static contextType = MyContext
998 * // For TS pre-3.7:
999 * context!: React.ContextType<typeof MyContext>
1000 * // For TS 3.7 and above:
1001 * declare context: React.ContextType<typeof MyContext>
1002 * ```
1003 *
1004 * @see {@link https://react.dev/reference/react/Component#context React Docs}
1005 */
1006 context: unknown;
1007
1008 constructor(props: P);
1009 /**
1010 * @deprecated
1011 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs}
1012 */
1013 constructor(props: P, context: any);
1014
1015 // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
1016 // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
1017 // Also, the ` | S` allows intellisense to not be dumbisense
1018 setState<K extends keyof S>(
1019 state: ((prevState: Readonly<S>, props: Readonly<P>) => Pick<S, K> | S | null) | (Pick<S, K> | S | null),
1020 callback?: () => void,
1021 ): void;
1022
1023 forceUpdate(callback?: () => void): void;
1024 render(): ReactNode;
1025
1026 readonly props: Readonly<P>;
1027 state: Readonly<S>;
1028 /**
1029 * @deprecated
1030 *
1031 * @see {@link https://legacy.reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs Legacy React Docs}
1032 */
1033 refs: {
1034 [key: string]: ReactInstance;
1035 };
1036 }
1037
1038 class PureComponent<P = {}, S = {}, SS = any> extends Component<P, S, SS> {}
1039
1040 /**
1041 * @deprecated Use `ClassicComponent` from `create-react-class`
1042 *
1043 * @see {@link https://legacy.reactjs.org/docs/react-without-es6.html Legacy React Docs}
1044 * @see {@link https://www.npmjs.com/package/create-react-class `create-react-class` on npm}
1045 */
1046 interface ClassicComponent<P = {}, S = {}> extends Component<P, S> {
1047 replaceState(nextState: S, callback?: () => void): void;
1048 isMounted(): boolean;
1049 getInitialState?(): S;
1050 }
1051
1052 interface ChildContextProvider<CC> {
1053 getChildContext(): CC;
1054 }
1055
1056 //
1057 // Class Interfaces
1058 // ----------------------------------------------------------------------
1059
1060 /**
1061 * Represents the type of a function component. Can optionally
1062 * receive a type argument that represents the props the component
1063 * receives.
1064 *
1065 * @template P The props the component accepts.
1066 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components React TypeScript Cheatsheet}
1067 * @alias for {@link FunctionComponent}
1068 *
1069 * @example
1070 *
1071 * ```tsx
1072 * // With props:
1073 * type Props = { name: string }
1074 *
1075 * const MyComponent: FC<Props> = (props) => {
1076 * return <div>{props.name}</div>
1077 * }
1078 * ```
1079 *
1080 * @example
1081 *
1082 * ```tsx
1083 * // Without props:
1084 * const MyComponentWithoutProps: FC = () => {
1085 * return <div>MyComponentWithoutProps</div>
1086 * }
1087 * ```
1088 */
1089 type FC<P = {}> = FunctionComponent<P>;
1090
1091 /**
1092 * Represents the type of a function component. Can optionally
1093 * receive a type argument that represents the props the component
1094 * accepts.
1095 *
1096 * @template P The props the component accepts.
1097 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components React TypeScript Cheatsheet}
1098 *
1099 * @example
1100 *
1101 * ```tsx
1102 * // With props:
1103 * type Props = { name: string }
1104 *
1105 * const MyComponent: FunctionComponent<Props> = (props) => {
1106 * return <div>{props.name}</div>
1107 * }
1108 * ```
1109 *
1110 * @example
1111 *
1112 * ```tsx
1113 * // Without props:
1114 * const MyComponentWithoutProps: FunctionComponent = () => {
1115 * return <div>MyComponentWithoutProps</div>
1116 * }
1117 * ```
1118 */
1119 interface FunctionComponent<P = {}> {
1120 (
1121 props: P,
1122 /**
1123 * @deprecated
1124 *
1125 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods React Docs}
1126 */
1127 deprecatedLegacyContext?: any,
1128 ): ReactNode;
1129 /**
1130 * Used to declare the types of the props accepted by the
1131 * component. These types will be checked during rendering
1132 * and in development only.
1133 *
1134 * We recommend using TypeScript instead of checking prop
1135 * types at runtime.
1136 *
1137 * @see {@link https://react.dev/reference/react/Component#static-proptypes React Docs}
1138 */
1139 propTypes?: WeakValidationMap<P> | undefined;
1140 /**
1141 * @deprecated
1142 *
1143 * Lets you specify which legacy context is consumed by
1144 * this component.
1145 *
1146 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html Legacy React Docs}
1147 */
1148 contextTypes?: ValidationMap<any> | undefined;
1149 /**
1150 * Used to define default values for the props accepted by
1151 * the component.
1152 *
1153 * @see {@link https://react.dev/reference/react/Component#static-defaultprops React Docs}
1154 *
1155 * @example
1156 *
1157 * ```tsx
1158 * type Props = { name?: string }
1159 *
1160 * const MyComponent: FC<Props> = (props) => {
1161 * return <div>{props.name}</div>
1162 * }
1163 *
1164 * MyComponent.defaultProps = {
1165 * name: 'John Doe'
1166 * }
1167 * ```
1168 *
1169 * @deprecated Use {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#default_value|default values for destructuring assignments instead}.
1170 */
1171 defaultProps?: Partial<P> | undefined;
1172 /**
1173 * Used in debugging messages. You might want to set it
1174 * explicitly if you want to display a different name for
1175 * debugging purposes.
1176 *
1177 * @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
1178 *
1179 * @example
1180 *
1181 * ```tsx
1182 *
1183 * const MyComponent: FC = () => {
1184 * return <div>Hello!</div>
1185 * }
1186 *
1187 * MyComponent.displayName = 'MyAwesomeComponent'
1188 * ```
1189 */
1190 displayName?: string | undefined;
1191 }
1192
1193 /**
1194 * @deprecated - Equivalent to {@link React.FunctionComponent}.
1195 *
1196 * @see {@link React.FunctionComponent}
1197 * @alias {@link VoidFunctionComponent}
1198 */
1199 type VFC<P = {}> = VoidFunctionComponent<P>;
1200
1201 /**
1202 * @deprecated - Equivalent to {@link React.FunctionComponent}.
1203 *
1204 * @see {@link React.FunctionComponent}
1205 */
1206 interface VoidFunctionComponent<P = {}> {
1207 (
1208 props: P,
1209 /**
1210 * @deprecated
1211 *
1212 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods React Docs}
1213 */
1214 deprecatedLegacyContext?: any,
1215 ): ReactNode;
1216 propTypes?: WeakValidationMap<P> | undefined;
1217 contextTypes?: ValidationMap<any> | undefined;
1218 /**
1219 * @deprecated Use {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#default_value|default values for destructuring assignments instead}.
1220 */
1221 defaultProps?: Partial<P> | undefined;
1222 displayName?: string | undefined;
1223 }
1224
1225 /**
1226 * The type of the ref received by a {@link ForwardRefRenderFunction}.
1227 *
1228 * @see {@link ForwardRefRenderFunction}
1229 */
1230 type ForwardedRef<T> = ((instance: T | null) => void) | MutableRefObject<T | null> | null;
1231
1232 /**
1233 * The type of the function passed to {@link forwardRef}. This is considered different
1234 * to a normal {@link FunctionComponent} because it receives an additional argument,
1235 *
1236 * @param props Props passed to the component, if any.
1237 * @param ref A ref forwarded to the component of type {@link ForwardedRef}.
1238 *
1239 * @template T The type of the forwarded ref.
1240 * @template P The type of the props the component accepts.
1241 *
1242 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forward_and_create_ref/ React TypeScript Cheatsheet}
1243 * @see {@link forwardRef}
1244 */
1245 interface ForwardRefRenderFunction<T, P = {}> {
1246 (props: P, ref: ForwardedRef<T>): ReactNode;
1247 /**
1248 * Used in debugging messages. You might want to set it
1249 * explicitly if you want to display a different name for
1250 * debugging purposes.
1251 *
1252 * Will show `ForwardRef(${Component.displayName || Component.name})`
1253 * in devtools by default, but can be given its own specific name.
1254 *
1255 * @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
1256 */
1257 displayName?: string | undefined;
1258 /**
1259 * defaultProps are not supported on render functions passed to forwardRef.
1260 *
1261 * @see {@link https://github.com/microsoft/TypeScript/issues/36826 linked GitHub issue} for context
1262 * @see {@link https://react.dev/reference/react/Component#static-defaultprops React Docs}
1263 */
1264 defaultProps?: never | undefined;
1265 /**
1266 * propTypes are not supported on render functions passed to forwardRef.
1267 *
1268 * @see {@link https://github.com/microsoft/TypeScript/issues/36826 linked GitHub issue} for context
1269 * @see {@link https://react.dev/reference/react/Component#static-proptypes React Docs}
1270 */
1271 propTypes?: never | undefined;
1272 }
1273
1274 /**
1275 * Represents a component class in React.
1276 *
1277 * @template P The props the component accepts.
1278 * @template S The internal state of the component.
1279 */
1280 interface ComponentClass<P = {}, S = ComponentState> extends StaticLifecycle<P, S> {
1281 new(
1282 props: P,
1283 /**
1284 * @deprecated
1285 *
1286 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods React Docs}
1287 */
1288 deprecatedLegacyContext?: any,
1289 ): Component<P, S>;
1290 /**
1291 * Used to declare the types of the props accepted by the
1292 * component. These types will be checked during rendering
1293 * and in development only.
1294 *
1295 * We recommend using TypeScript instead of checking prop
1296 * types at runtime.
1297 *
1298 * @see {@link https://react.dev/reference/react/Component#static-proptypes React Docs}
1299 */
1300 propTypes?: WeakValidationMap<P> | undefined;
1301 contextType?: Context<any> | undefined;
1302 /**
1303 * @deprecated use {@link ComponentClass.contextType} instead
1304 *
1305 * Lets you specify which legacy context is consumed by
1306 * this component.
1307 *
1308 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html Legacy React Docs}
1309 */
1310 contextTypes?: ValidationMap<any> | undefined;
1311 /**
1312 * @deprecated
1313 *
1314 * @see {@link https://legacy.reactjs.org/docs/legacy-context.html#how-to-use-context Legacy React Docs}
1315 */
1316 childContextTypes?: ValidationMap<any> | undefined;
1317 /**
1318 * Used to define default values for the props accepted by
1319 * the component.
1320 *
1321 * @see {@link https://react.dev/reference/react/Component#static-defaultprops React Docs}
1322 */
1323 defaultProps?: Partial<P> | undefined;
1324 /**
1325 * Used in debugging messages. You might want to set it
1326 * explicitly if you want to display a different name for
1327 * debugging purposes.
1328 *
1329 * @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
1330 */
1331 displayName?: string | undefined;
1332 }
1333
1334 /**
1335 * @deprecated Use `ClassicComponentClass` from `create-react-class`
1336 *
1337 * @see {@link https://legacy.reactjs.org/docs/react-without-es6.html Legacy React Docs}
1338 * @see {@link https://www.npmjs.com/package/create-react-class `create-react-class` on npm}
1339 */
1340 interface ClassicComponentClass<P = {}> extends ComponentClass<P> {
1341 new(props: P, deprecatedLegacyContext?: any): ClassicComponent<P, ComponentState>;
1342 getDefaultProps?(): P;
1343 }
1344
1345 /**
1346 * Used in {@link createElement} and {@link createFactory} to represent
1347 * a class.
1348 *
1349 * An intersection type is used to infer multiple type parameters from
1350 * a single argument, which is useful for many top-level API defs.
1351 * See {@link https://github.com/Microsoft/TypeScript/issues/7234 this GitHub issue}
1352 * for more info.
1353 */
1354 type ClassType<P, T extends Component<P, ComponentState>, C extends ComponentClass<P>> =
1355 & C
1356 & (new(props: P, deprecatedLegacyContext?: any) => T);
1357
1358 //
1359 // Component Specs and Lifecycle
1360 // ----------------------------------------------------------------------
1361
1362 // This should actually be something like `Lifecycle<P, S> | DeprecatedLifecycle<P, S>`,
1363 // as React will _not_ call the deprecated lifecycle methods if any of the new lifecycle
1364 // methods are present.
1365 interface ComponentLifecycle<P, S, SS = any> extends NewLifecycle<P, S, SS>, DeprecatedLifecycle<P, S> {
1366 /**
1367 * Called immediately after a component is mounted. Setting state here will trigger re-rendering.
1368 */
1369 componentDidMount?(): void;
1370 /**
1371 * Called to determine whether the change in props and state should trigger a re-render.
1372 *
1373 * `Component` always returns true.
1374 * `PureComponent` implements a shallow comparison on props and state and returns true if any
1375 * props or states have changed.
1376 *
1377 * If false is returned, {@link Component.render}, `componentWillUpdate`
1378 * and `componentDidUpdate` will not be called.
1379 */
1380 shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
1381 /**
1382 * Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as
1383 * cancelled network requests, or cleaning up any DOM elements created in `componentDidMount`.
1384 */
1385 componentWillUnmount?(): void;
1386 /**
1387 * Catches exceptions generated in descendant components. Unhandled exceptions will cause
1388 * the entire component tree to unmount.
1389 */
1390 componentDidCatch?(error: Error, errorInfo: ErrorInfo): void;
1391 }
1392
1393 // Unfortunately, we have no way of declaring that the component constructor must implement this
1394 interface StaticLifecycle<P, S> {
1395 getDerivedStateFromProps?: GetDerivedStateFromProps<P, S> | undefined;
1396 getDerivedStateFromError?: GetDerivedStateFromError<P, S> | undefined;
1397 }
1398
1399 type GetDerivedStateFromProps<P, S> =
1400 /**
1401 * Returns an update to a component's state based on its new props and old state.
1402 *
1403 * Note: its presence prevents any of the deprecated lifecycle methods from being invoked
1404 */
1405 (nextProps: Readonly<P>, prevState: S) => Partial<S> | null;
1406
1407 type GetDerivedStateFromError<P, S> =
1408 /**
1409 * This lifecycle is invoked after an error has been thrown by a descendant component.
1410 * It receives the error that was thrown as a parameter and should return a value to update state.
1411 *
1412 * Note: its presence prevents any of the deprecated lifecycle methods from being invoked
1413 */
1414 (error: any) => Partial<S> | null;
1415
1416 // This should be "infer SS" but can't use it yet
1417 interface NewLifecycle<P, S, SS> {
1418 /**
1419 * Runs before React applies the result of {@link Component.render render} to the document, and
1420 * returns an object to be given to {@link componentDidUpdate}. Useful for saving
1421 * things such as scroll position before {@link Component.render render} causes changes to it.
1422 *
1423 * Note: the presence of this method prevents any of the deprecated
1424 * lifecycle events from running.
1425 */
1426 getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>): SS | null;
1427 /**
1428 * Called immediately after updating occurs. Not called for the initial render.
1429 *
1430 * The snapshot is only present if {@link getSnapshotBeforeUpdate} is present and returns non-null.
1431 */
1432 componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot?: SS): void;
1433 }
1434
1435 interface DeprecatedLifecycle<P, S> {
1436 /**
1437 * Called immediately before mounting occurs, and before {@link Component.render}.
1438 * Avoid introducing any side-effects or subscriptions in this method.
1439 *
1440 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1441 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1442 * this from being invoked.
1443 *
1444 * @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead; will stop working in React 17
1445 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
1446 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1447 */
1448 componentWillMount?(): void;
1449 /**
1450 * Called immediately before mounting occurs, and before {@link Component.render}.
1451 * Avoid introducing any side-effects or subscriptions in this method.
1452 *
1453 * This method will not stop working in React 17.
1454 *
1455 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1456 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1457 * this from being invoked.
1458 *
1459 * @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead
1460 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
1461 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1462 */
1463 UNSAFE_componentWillMount?(): void;
1464 /**
1465 * Called when the component may be receiving new props.
1466 * React may call this even if props have not changed, so be sure to compare new and existing
1467 * props if you only want to handle changes.
1468 *
1469 * Calling {@link Component.setState} generally does not trigger this method.
1470 *
1471 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1472 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1473 * this from being invoked.
1474 *
1475 * @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead; will stop working in React 17
1476 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
1477 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1478 */
1479 componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
1480 /**
1481 * Called when the component may be receiving new props.
1482 * React may call this even if props have not changed, so be sure to compare new and existing
1483 * props if you only want to handle changes.
1484 *
1485 * Calling {@link Component.setState} generally does not trigger this method.
1486 *
1487 * This method will not stop working in React 17.
1488 *
1489 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1490 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1491 * this from being invoked.
1492 *
1493 * @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead
1494 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
1495 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1496 */
1497 UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
1498 /**
1499 * Called immediately before rendering when new props or state is received. Not called for the initial render.
1500 *
1501 * Note: You cannot call {@link Component.setState} here.
1502 *
1503 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1504 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1505 * this from being invoked.
1506 *
1507 * @deprecated 16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17
1508 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
1509 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1510 */
1511 componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
1512 /**
1513 * Called immediately before rendering when new props or state is received. Not called for the initial render.
1514 *
1515 * Note: You cannot call {@link Component.setState} here.
1516 *
1517 * This method will not stop working in React 17.
1518 *
1519 * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1520 * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1521 * this from being invoked.
1522 *
1523 * @deprecated 16.3, use getSnapshotBeforeUpdate instead
1524 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
1525 * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1526 */
1527 UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
1528 }
1529
1530 /**
1531 * @deprecated
1532 *
1533 * @see {@link https://legacy.reactjs.org/blog/2016/07/13/mixins-considered-harmful.html Mixins Considered Harmful}
1534 */
1535 interface Mixin<P, S> extends ComponentLifecycle<P, S> {
1536 mixins?: Array<Mixin<P, S>> | undefined;
1537 statics?: {
1538 [key: string]: any;
1539 } | undefined;
1540
1541 displayName?: string | undefined;
1542 propTypes?: ValidationMap<any> | undefined;
1543 contextTypes?: ValidationMap<any> | undefined;
1544 childContextTypes?: ValidationMap<any> | undefined;
1545
1546 getDefaultProps?(): P;
1547 getInitialState?(): S;
1548 }
1549
1550 /**
1551 * @deprecated
1552 *
1553 * @see {@link https://legacy.reactjs.org/blog/2016/07/13/mixins-considered-harmful.html Mixins Considered Harmful}
1554 */
1555 interface ComponentSpec<P, S> extends Mixin<P, S> {
1556 render(): ReactNode;
1557
1558 [propertyName: string]: any;
1559 }
1560
1561 function createRef<T>(): RefObject<T>;
1562
1563 /**
1564 * The type of the component returned from {@link forwardRef}.
1565 *
1566 * @template P The props the component accepts, if any.
1567 *
1568 * @see {@link ExoticComponent}
1569 */
1570 interface ForwardRefExoticComponent<P> extends NamedExoticComponent<P> {
1571 /**
1572 * @deprecated Use {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#default_value|default values for destructuring assignments instead}.
1573 */
1574 defaultProps?: Partial<P> | undefined;
1575 propTypes?: WeakValidationMap<P> | undefined;
1576 }
1577
1578 /**
1579 * Lets your component expose a DOM node to a parent component
1580 * using a ref.
1581 *
1582 * @see {@link https://react.dev/reference/react/forwardRef React Docs}
1583 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forward_and_create_ref/ React TypeScript Cheatsheet}
1584 *
1585 * @param render See the {@link ForwardRefRenderFunction}.
1586 *
1587 * @template T The type of the DOM node.
1588 * @template P The props the component accepts, if any.
1589 *
1590 * @example
1591 *
1592 * ```tsx
1593 * interface Props {
1594 * children?: ReactNode;
1595 * type: "submit" | "button";
1596 * }
1597 *
1598 * export const FancyButton = forwardRef<HTMLButtonElement, Props>((props, ref) => (
1599 * <button ref={ref} className="MyClassName" type={props.type}>
1600 * {props.children}
1601 * </button>
1602 * ));
1603 * ```
1604 */
1605 function forwardRef<T, P = {}>(
1606 render: ForwardRefRenderFunction<T, PropsWithoutRef<P>>,
1607 ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
1608
1609 /**
1610 * Omits the 'ref' attribute from the given props object.
1611 *
1612 * @template P The props object type.
1613 */
1614 type PropsWithoutRef<P> =
1615 // Omit would not be sufficient for this. We'd like to avoid unnecessary mapping and need a distributive conditional to support unions.
1616 // see: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
1617 // https://github.com/Microsoft/TypeScript/issues/28339
1618 P extends any ? ("ref" extends keyof P ? Omit<P, "ref"> : P) : P;
1619 /** Ensures that the props do not include string ref, which cannot be forwarded */
1620 type PropsWithRef<P> =
1621 // Note: String refs can be forwarded. We can't fix this bug without breaking a bunch of libraries now though.
1622 // Just "P extends { ref?: infer R }" looks sufficient, but R will infer as {} if P is {}.
1623 "ref" extends keyof P
1624 ? P extends { ref?: infer R | undefined }
1625 ? string extends R ? PropsWithoutRef<P> & { ref?: Exclude<R, string> | undefined }
1626 : P
1627 : P
1628 : P;
1629
1630 type PropsWithChildren<P = unknown> = P & { children?: ReactNode | undefined };
1631
1632 /**
1633 * Used to retrieve the props a component accepts. Can either be passed a string,
1634 * indicating a DOM element (e.g. 'div', 'span', etc.) or the type of a React
1635 * component.
1636 *
1637 * It's usually better to use {@link ComponentPropsWithRef} or {@link ComponentPropsWithoutRef}
1638 * instead of this type, as they let you be explicit about whether or not to include
1639 * the `ref` prop.
1640 *
1641 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/componentprops/ React TypeScript Cheatsheet}
1642 *
1643 * @example
1644 *
1645 * ```tsx
1646 * // Retrieves the props an 'input' element accepts
1647 * type InputProps = React.ComponentProps<'input'>;
1648 * ```
1649 *
1650 * @example
1651 *
1652 * ```tsx
1653 * const MyComponent = (props: { foo: number, bar: string }) => <div />;
1654 *
1655 * // Retrieves the props 'MyComponent' accepts
1656 * type MyComponentProps = React.ComponentProps<typeof MyComponent>;
1657 * ```
1658 */
1659 type ComponentProps<T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>> = T extends
1660 JSXElementConstructor<infer P> ? P
1661 : T extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[T]
1662 : {};
1663
1664 /**
1665 * Used to retrieve the props a component accepts with its ref. Can either be
1666 * passed a string, indicating a DOM element (e.g. 'div', 'span', etc.) or the
1667 * type of a React component.
1668 *
1669 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/componentprops/ React TypeScript Cheatsheet}
1670 *
1671 * @example
1672 *
1673 * ```tsx
1674 * // Retrieves the props an 'input' element accepts
1675 * type InputProps = React.ComponentPropsWithRef<'input'>;
1676 * ```
1677 *
1678 * @example
1679 *
1680 * ```tsx
1681 * const MyComponent = (props: { foo: number, bar: string }) => <div />;
1682 *
1683 * // Retrieves the props 'MyComponent' accepts
1684 * type MyComponentPropsWithRef = React.ComponentPropsWithRef<typeof MyComponent>;
1685 * ```
1686 */
1687 type ComponentPropsWithRef<T extends ElementType> = T extends (new(props: infer P) => Component<any, any>)
1688 ? PropsWithoutRef<P> & RefAttributes<InstanceType<T>>
1689 : PropsWithRef<ComponentProps<T>>;
1690 /**
1691 * Used to retrieve the props a custom component accepts with its ref.
1692 *
1693 * Unlike {@link ComponentPropsWithRef}, this only works with custom
1694 * components, i.e. components you define yourself. This is to improve
1695 * type-checking performance.
1696 *
1697 * @example
1698 *
1699 * ```tsx
1700 * const MyComponent = (props: { foo: number, bar: string }) => <div />;
1701 *
1702 * // Retrieves the props 'MyComponent' accepts
1703 * type MyComponentPropsWithRef = React.CustomComponentPropsWithRef<typeof MyComponent>;
1704 * ```
1705 */
1706 type CustomComponentPropsWithRef<T extends ComponentType> = T extends (new(props: infer P) => Component<any, any>)
1707 ? (PropsWithoutRef<P> & RefAttributes<InstanceType<T>>)
1708 : T extends ((props: infer P, legacyContext?: any) => ReactNode) ? PropsWithRef<P>
1709 : never;
1710
1711 /**
1712 * Used to retrieve the props a component accepts without its ref. Can either be
1713 * passed a string, indicating a DOM element (e.g. 'div', 'span', etc.) or the
1714 * type of a React component.
1715 *
1716 * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/componentprops/ React TypeScript Cheatsheet}
1717 *
1718 * @example
1719 *
1720 * ```tsx
1721 * // Retrieves the props an 'input' element accepts
1722 * type InputProps = React.ComponentPropsWithoutRef<'input'>;
1723 * ```
1724 *
1725 * @example
1726 *
1727 * ```tsx
1728 * const MyComponent = (props: { foo: number, bar: string }) => <div />;
1729 *
1730 * // Retrieves the props 'MyComponent' accepts
1731 * type MyComponentPropsWithoutRef = React.ComponentPropsWithoutRef<typeof MyComponent>;
1732 * ```
1733 */
1734 type ComponentPropsWithoutRef<T extends ElementType> = PropsWithoutRef<ComponentProps<T>>;
1735
1736 type ComponentRef<T extends ElementType> = T extends NamedExoticComponent<
1737 ComponentPropsWithoutRef<T> & RefAttributes<infer Method>
1738 > ? Method
1739 : ComponentPropsWithRef<T> extends RefAttributes<infer Method> ? Method
1740 : never;
1741
1742 // will show `Memo(${Component.displayName || Component.name})` in devtools by default,
1743 // but can be given its own specific name
1744 type MemoExoticComponent<T extends ComponentType<any>> = NamedExoticComponent<CustomComponentPropsWithRef<T>> & {
1745 readonly type: T;
1746 };
1747
1748 /**
1749 * Lets you skip re-rendering a component when its props are unchanged.
1750 *
1751 * @see {@link https://react.dev/reference/react/memo React Docs}
1752 *
1753 * @param Component The component to memoize.
1754 * @param propsAreEqual A function that will be used to determine if the props have changed.
1755 *
1756 * @example
1757 *
1758 * ```tsx
1759 * import { memo } from 'react';
1760 *
1761 * const SomeComponent = memo(function SomeComponent(props: { foo: string }) {
1762 * // ...
1763 * });
1764 * ```
1765 */
1766 function memo<P extends object>(
1767 Component: FunctionComponent<P>,
1768 propsAreEqual?: (prevProps: Readonly<P>, nextProps: Readonly<P>) => boolean,
1769 ): NamedExoticComponent<P>;
1770 function memo<T extends ComponentType<any>>(
1771 Component: T,
1772 propsAreEqual?: (prevProps: Readonly<ComponentProps<T>>, nextProps: Readonly<ComponentProps<T>>) => boolean,
1773 ): MemoExoticComponent<T>;
1774
1775 interface LazyExoticComponent<T extends ComponentType<any>>
1776 extends ExoticComponent<CustomComponentPropsWithRef<T>>
1777 {
1778 readonly _result: T;
1779 }
1780
1781 /**
1782 * Lets you defer loading a component’s code until it is rendered for the first time.
1783 *
1784 * @see {@link https://react.dev/reference/react/lazy React Docs}
1785 *
1786 * @param load A function that returns a `Promise` or another thenable (a `Promise`-like object with a
1787 * then method). React will not call `load` until the first time you attempt to render the returned
1788 * component. After React first calls load, it will wait for it to resolve, and then render the
1789 * resolved value’s `.default` as a React component. Both the returned `Promise` and the `Promise`’s
1790 * resolved value will be cached, so React will not call load more than once. If the `Promise` rejects,
1791 * React will throw the rejection reason for the nearest Error Boundary to handle.
1792 *
1793 * @example
1794 *
1795 * ```tsx
1796 * import { lazy } from 'react';
1797 *
1798 * const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
1799 * ```
1800 */
1801 function lazy<T extends ComponentType<any>>(
1802 load: () => Promise<{ default: T }>,
1803 ): LazyExoticComponent<T>;
1804
1805 //
1806 // React Hooks
1807 // ----------------------------------------------------------------------
1808
1809 /**
1810 * The instruction passed to a {@link Dispatch} function in {@link useState}
1811 * to tell React what the next value of the {@link useState} should be.
1812 *
1813 * Often found wrapped in {@link Dispatch}.
1814 *
1815 * @template S The type of the state.
1816 *
1817 * @example
1818 *
1819 * ```tsx
1820 * // This return type correctly represents the type of
1821 * // `setCount` in the example below.
1822 * const useCustomState = (): Dispatch<SetStateAction<number>> => {
1823 * const [count, setCount] = useState(0);
1824 *
1825 * return setCount;
1826 * }
1827 * ```
1828 */
1829 type SetStateAction<S> = S | ((prevState: S) => S);
1830
1831 /**
1832 * A function that can be used to update the state of a {@link useState}
1833 * or {@link useReducer} hook.
1834 */
1835 type Dispatch<A> = (value: A) => void;
1836 /**
1837 * A {@link Dispatch} function can sometimes be called without any arguments.
1838 */
1839 type DispatchWithoutAction = () => void;
1840 // Unlike redux, the actions _can_ be anything
1841 type Reducer<S, A> = (prevState: S, action: A) => S;
1842 // If useReducer accepts a reducer without action, dispatch may be called without any parameters.
1843 type ReducerWithoutAction<S> = (prevState: S) => S;
1844 // types used to try and prevent the compiler from reducing S
1845 // to a supertype common with the second argument to useReducer()
1846 type ReducerState<R extends Reducer<any, any>> = R extends Reducer<infer S, any> ? S : never;
1847 type ReducerAction<R extends Reducer<any, any>> = R extends Reducer<any, infer A> ? A : never;
1848 // The identity check is done with the SameValue algorithm (Object.is), which is stricter than ===
1849 type ReducerStateWithoutAction<R extends ReducerWithoutAction<any>> = R extends ReducerWithoutAction<infer S> ? S
1850 : never;
1851 type DependencyList = readonly unknown[];
1852
1853 // NOTE: callbacks are _only_ allowed to return either void, or a destructor.
1854 type EffectCallback = () => void | Destructor;
1855
1856 interface MutableRefObject<T> {
1857 current: T;
1858 }
1859
1860 // This will technically work if you give a Consumer<T> or Provider<T> but it's deprecated and warns
1861 /**
1862 * Accepts a context object (the value returned from `React.createContext`) and returns the current
1863 * context value, as given by the nearest context provider for the given context.
1864 *
1865 * @version 16.8.0
1866 * @see {@link https://react.dev/reference/react/useContext}
1867 */
1868 function useContext<T>(context: Context<T> /*, (not public API) observedBits?: number|boolean */): T;
1869 /**
1870 * Returns a stateful value, and a function to update it.
1871 *
1872 * @version 16.8.0
1873 * @see {@link https://react.dev/reference/react/useState}
1874 */
1875 function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
1876 // convenience overload when first argument is omitted
1877 /**
1878 * Returns a stateful value, and a function to update it.
1879 *
1880 * @version 16.8.0
1881 * @see {@link https://react.dev/reference/react/useState}
1882 */
1883 function useState<S = undefined>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>];
1884 /**
1885 * An alternative to `useState`.
1886 *
1887 * `useReducer` is usually preferable to `useState` when you have complex state logic that involves
1888 * multiple sub-values. It also lets you optimize performance for components that trigger deep
1889 * updates because you can pass `dispatch` down instead of callbacks.
1890 *
1891 * @version 16.8.0
1892 * @see {@link https://react.dev/reference/react/useReducer}
1893 */
1894 // overload where dispatch could accept 0 arguments.
1895 function useReducer<R extends ReducerWithoutAction<any>, I>(
1896 reducer: R,
1897 initializerArg: I,
1898 initializer: (arg: I) => ReducerStateWithoutAction<R>,
1899 ): [ReducerStateWithoutAction<R>, DispatchWithoutAction];
1900 /**
1901 * An alternative to `useState`.
1902 *
1903 * `useReducer` is usually preferable to `useState` when you have complex state logic that involves
1904 * multiple sub-values. It also lets you optimize performance for components that trigger deep
1905 * updates because you can pass `dispatch` down instead of callbacks.
1906 *
1907 * @version 16.8.0
1908 * @see {@link https://react.dev/reference/react/useReducer}
1909 */
1910 // overload where dispatch could accept 0 arguments.
1911 function useReducer<R extends ReducerWithoutAction<any>>(
1912 reducer: R,
1913 initializerArg: ReducerStateWithoutAction<R>,
1914 initializer?: undefined,
1915 ): [ReducerStateWithoutAction<R>, DispatchWithoutAction];
1916 /**
1917 * An alternative to `useState`.
1918 *
1919 * `useReducer` is usually preferable to `useState` when you have complex state logic that involves
1920 * multiple sub-values. It also lets you optimize performance for components that trigger deep
1921 * updates because you can pass `dispatch` down instead of callbacks.
1922 *
1923 * @version 16.8.0
1924 * @see {@link https://react.dev/reference/react/useReducer}
1925 */
1926 // overload where "I" may be a subset of ReducerState<R>; used to provide autocompletion.
1927 // If "I" matches ReducerState<R> exactly then the last overload will allow initializer to be omitted.
1928 // the last overload effectively behaves as if the identity function (x => x) is the initializer.
1929 function useReducer<R extends Reducer<any, any>, I>(
1930 reducer: R,
1931 initializerArg: I & ReducerState<R>,
1932 initializer: (arg: I & ReducerState<R>) => ReducerState<R>,
1933 ): [ReducerState<R>, Dispatch<ReducerAction<R>>];
1934 /**
1935 * An alternative to `useState`.
1936 *
1937 * `useReducer` is usually preferable to `useState` when you have complex state logic that involves
1938 * multiple sub-values. It also lets you optimize performance for components that trigger deep
1939 * updates because you can pass `dispatch` down instead of callbacks.
1940 *
1941 * @version 16.8.0
1942 * @see {@link https://react.dev/reference/react/useReducer}
1943 */
1944 // overload for free "I"; all goes as long as initializer converts it into "ReducerState<R>".
1945 function useReducer<R extends Reducer<any, any>, I>(
1946 reducer: R,
1947 initializerArg: I,
1948 initializer: (arg: I) => ReducerState<R>,
1949 ): [ReducerState<R>, Dispatch<ReducerAction<R>>];
1950 /**
1951 * An alternative to `useState`.
1952 *
1953 * `useReducer` is usually preferable to `useState` when you have complex state logic that involves
1954 * multiple sub-values. It also lets you optimize performance for components that trigger deep
1955 * updates because you can pass `dispatch` down instead of callbacks.
1956 *
1957 * @version 16.8.0
1958 * @see {@link https://react.dev/reference/react/useReducer}
1959 */
1960
1961 // I'm not sure if I keep this 2-ary or if I make it (2,3)-ary; it's currently (2,3)-ary.
1962 // The Flow types do have an overload for 3-ary invocation with undefined initializer.
1963
1964 // NOTE: without the ReducerState indirection, TypeScript would reduce S to be the most common
1965 // supertype between the reducer's return type and the initialState (or the initializer's return type),
1966 // which would prevent autocompletion from ever working.
1967
1968 // TODO: double-check if this weird overload logic is necessary. It is possible it's either a bug
1969 // in older versions, or a regression in newer versions of the typescript completion service.
1970 function useReducer<R extends Reducer<any, any>>(
1971 reducer: R,
1972 initialState: ReducerState<R>,
1973 initializer?: undefined,
1974 ): [ReducerState<R>, Dispatch<ReducerAction<R>>];
1975 /**
1976 * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
1977 * (`initialValue`). The returned object will persist for the full lifetime of the component.
1978 *
1979 * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
1980 * value around similar to how you’d use instance fields in classes.
1981 *
1982 * @version 16.8.0
1983 * @see {@link https://react.dev/reference/react/useRef}
1984 */
1985 function useRef<T>(initialValue: T): MutableRefObject<T>;
1986 // convenience overload for refs given as a ref prop as they typically start with a null value
1987 /**
1988 * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
1989 * (`initialValue`). The returned object will persist for the full lifetime of the component.
1990 *
1991 * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
1992 * value around similar to how you’d use instance fields in classes.
1993 *
1994 * Usage note: if you need the result of useRef to be directly mutable, include `| null` in the type
1995 * of the generic argument.
1996 *
1997 * @version 16.8.0
1998 * @see {@link https://react.dev/reference/react/useRef}
1999 */
2000 function useRef<T>(initialValue: T | null): RefObject<T>;
2001 // convenience overload for potentially undefined initialValue / call with 0 arguments
2002 // has a default to stop it from defaulting to {} instead
2003 /**
2004 * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
2005 * (`initialValue`). The returned object will persist for the full lifetime of the component.
2006 *
2007 * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
2008 * value around similar to how you’d use instance fields in classes.
2009 *
2010 * @version 16.8.0
2011 * @see {@link https://react.dev/reference/react/useRef}
2012 */
2013 function useRef<T = undefined>(): MutableRefObject<T | undefined>;
2014 /**
2015 * The signature is identical to `useEffect`, but it fires synchronously after all DOM mutations.
2016 * Use this to read layout from the DOM and synchronously re-render. Updates scheduled inside
2017 * `useLayoutEffect` will be flushed synchronously, before the browser has a chance to paint.
2018 *
2019 * Prefer the standard `useEffect` when possible to avoid blocking visual updates.
2020 *
2021 * If you’re migrating code from a class component, `useLayoutEffect` fires in the same phase as
2022 * `componentDidMount` and `componentDidUpdate`.
2023 *
2024 * @version 16.8.0
2025 * @see {@link https://react.dev/reference/react/useLayoutEffect}
2026 */
2027 function useLayoutEffect(effect: EffectCallback, deps?: DependencyList): void;
2028 /**
2029 * Accepts a function that contains imperative, possibly effectful code.
2030 *
2031 * @param effect Imperative function that can return a cleanup function
2032 * @param deps If present, effect will only activate if the values in the list change.
2033 *
2034 * @version 16.8.0
2035 * @see {@link https://react.dev/reference/react/useEffect}
2036 */
2037 function useEffect(effect: EffectCallback, deps?: DependencyList): void;
2038 // NOTE: this does not accept strings, but this will have to be fixed by removing strings from type Ref<T>
2039 /**
2040 * `useImperativeHandle` customizes the instance value that is exposed to parent components when using
2041 * `ref`. As always, imperative code using refs should be avoided in most cases.
2042 *
2043 * `useImperativeHandle` should be used with `React.forwardRef`.
2044 *
2045 * @version 16.8.0
2046 * @see {@link https://react.dev/reference/react/useImperativeHandle}
2047 */
2048 function useImperativeHandle<T, R extends T>(ref: Ref<T> | undefined, init: () => R, deps?: DependencyList): void;
2049 // I made 'inputs' required here and in useMemo as there's no point to memoizing without the memoization key
2050 // useCallback(X) is identical to just using X, useMemo(() => Y) is identical to just using Y.
2051 /**
2052 * `useCallback` will return a memoized version of the callback that only changes if one of the `inputs`
2053 * has changed.
2054 *
2055 * @version 16.8.0
2056 * @see {@link https://react.dev/reference/react/useCallback}
2057 */
2058 // A specific function type would not trigger implicit any.
2059 // See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/52873#issuecomment-845806435 for a comparison between `Function` and more specific types.
2060 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
2061 function useCallback<T extends Function>(callback: T, deps: DependencyList): T;
2062 /**
2063 * `useMemo` will only recompute the memoized value when one of the `deps` has changed.
2064 *
2065 * @version 16.8.0
2066 * @see {@link https://react.dev/reference/react/useMemo}
2067 */
2068 // allow undefined, but don't make it optional as that is very likely a mistake
2069 function useMemo<T>(factory: () => T, deps: DependencyList): T;
2070 /**
2071 * `useDebugValue` can be used to display a label for custom hooks in React DevTools.
2072 *
2073 * NOTE: We don’t recommend adding debug values to every custom hook.
2074 * It’s most valuable for custom hooks that are part of shared libraries.
2075 *
2076 * @version 16.8.0
2077 * @see {@link https://react.dev/reference/react/useDebugValue}
2078 */
2079 // the name of the custom hook is itself derived from the function name at runtime:
2080 // it's just the function name without the "use" prefix.
2081 function useDebugValue<T>(value: T, format?: (value: T) => any): void;
2082
2083 // must be synchronous
2084 export type TransitionFunction = () => VoidOrUndefinedOnly;
2085 // strange definition to allow vscode to show documentation on the invocation
2086 export interface TransitionStartFunction {
2087 /**
2088 * State updates caused inside the callback are allowed to be deferred.
2089 *
2090 * **If some state update causes a component to suspend, that state update should be wrapped in a transition.**
2091 *
2092 * @param callback A _synchronous_ function which causes state updates that can be deferred.
2093 */
2094 (callback: TransitionFunction): void;
2095 }
2096
2097 /**
2098 * Returns a deferred version of the value that may “lag behind” it.
2099 *
2100 * This is commonly used to keep the interface responsive when you have something that renders immediately
2101 * based on user input and something that needs to wait for a data fetch.
2102 *
2103 * A good example of this is a text input.
2104 *
2105 * @param value The value that is going to be deferred
2106 *
2107 * @see {@link https://react.dev/reference/react/useDeferredValue}
2108 */
2109 export function useDeferredValue<T>(value: T): T;
2110
2111 /**
2112 * Allows components to avoid undesirable loading states by waiting for content to load
2113 * before transitioning to the next screen. It also allows components to defer slower,
2114 * data fetching updates until subsequent renders so that more crucial updates can be
2115 * rendered immediately.
2116 *
2117 * The `useTransition` hook returns two values in an array.
2118 *
2119 * The first is a boolean, React’s way of informing us whether we’re waiting for the transition to finish.
2120 * The second is a function that takes a callback. We can use it to tell React which state we want to defer.
2121 *
2122 * **If some state update causes a component to suspend, that state update should be wrapped in a transition.**
2123 *
2124 * @see {@link https://react.dev/reference/react/useTransition}
2125 */
2126 export function useTransition(): [boolean, TransitionStartFunction];
2127
2128 /**
2129 * Similar to `useTransition` but allows uses where hooks are not available.
2130 *
2131 * @param callback A _synchronous_ function which causes state updates that can be deferred.
2132 */
2133 export function startTransition(scope: TransitionFunction): void;
2134
2135 /**
2136 * Wrap any code rendering and triggering updates to your components into `act()` calls.
2137 *
2138 * Ensures that the behavior in your tests matches what happens in the browser
2139 * more closely by executing pending `useEffect`s before returning. This also
2140 * reduces the amount of re-renders done.
2141 *
2142 * @param callback A synchronous, void callback that will execute as a single, complete React commit.
2143 *
2144 * @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
2145 */
2146 // While act does always return Thenable, if a void function is passed, we pretend the return value is also void to not trigger dangling Promise lint rules.
2147 export function act(callback: () => VoidOrUndefinedOnly): void;
2148 export function act<T>(callback: () => T | Promise<T>): Promise<T>;
2149
2150 export function useId(): string;
2151
2152 /**
2153 * @param effect Imperative function that can return a cleanup function
2154 * @param deps If present, effect will only activate if the values in the list change.
2155 *
2156 * @see {@link https://github.com/facebook/react/pull/21913}
2157 */
2158 export function useInsertionEffect(effect: EffectCallback, deps?: DependencyList): void;
2159
2160 /**
2161 * @param subscribe
2162 * @param getSnapshot
2163 *
2164 * @see {@link https://github.com/reactwg/react-18/discussions/86}
2165 */
2166 // keep in sync with `useSyncExternalStore` from `use-sync-external-store`
2167 export function useSyncExternalStore<Snapshot>(
2168 subscribe: (onStoreChange: () => void) => () => void,
2169 getSnapshot: () => Snapshot,
2170 getServerSnapshot?: () => Snapshot,
2171 ): Snapshot;
2172
2173 //
2174 // Event System
2175 // ----------------------------------------------------------------------
2176 // TODO: change any to unknown when moving to TS v3
2177 interface BaseSyntheticEvent<E = object, C = any, T = any> {
2178 nativeEvent: E;
2179 currentTarget: C;
2180 target: T;
2181 bubbles: boolean;
2182 cancelable: boolean;
2183 defaultPrevented: boolean;
2184 eventPhase: number;
2185 isTrusted: boolean;
2186 preventDefault(): void;
2187 isDefaultPrevented(): boolean;
2188 stopPropagation(): void;
2189 isPropagationStopped(): boolean;
2190 persist(): void;
2191 timeStamp: number;
2192 type: string;
2193 }
2194
2195 /**
2196 * currentTarget - a reference to the element on which the event listener is registered.
2197 *
2198 * target - a reference to the element from which the event was originally dispatched.
2199 * This might be a child element to the element on which the event listener is registered.
2200 * If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11508#issuecomment-256045682
2201 */
2202 interface SyntheticEvent<T = Element, E = Event> extends BaseSyntheticEvent<E, EventTarget & T, EventTarget> {}
2203
2204 interface ClipboardEvent<T = Element> extends SyntheticEvent<T, NativeClipboardEvent> {
2205 clipboardData: DataTransfer;
2206 }
2207
2208 interface CompositionEvent<T = Element> extends SyntheticEvent<T, NativeCompositionEvent> {
2209 data: string;
2210 }
2211
2212 interface DragEvent<T = Element> extends MouseEvent<T, NativeDragEvent> {
2213 dataTransfer: DataTransfer;
2214 }
2215
2216 interface PointerEvent<T = Element> extends MouseEvent<T, NativePointerEvent> {
2217 pointerId: number;
2218 pressure: number;
2219 tangentialPressure: number;
2220 tiltX: number;
2221 tiltY: number;
2222 twist: number;
2223 width: number;
2224 height: number;
2225 pointerType: "mouse" | "pen" | "touch";
2226 isPrimary: boolean;
2227 }
2228
2229 interface FocusEvent<Target = Element, RelatedTarget = Element> extends SyntheticEvent<Target, NativeFocusEvent> {
2230 relatedTarget: (EventTarget & RelatedTarget) | null;
2231 target: EventTarget & Target;
2232 }
2233
2234 interface FormEvent<T = Element> extends SyntheticEvent<T> {
2235 }
2236
2237 interface InvalidEvent<T = Element> extends SyntheticEvent<T> {
2238 target: EventTarget & T;
2239 }
2240
2241 interface ChangeEvent<T = Element> extends SyntheticEvent<T> {
2242 target: EventTarget & T;
2243 }
2244
2245 export type ModifierKey =
2246 | "Alt"
2247 | "AltGraph"
2248 | "CapsLock"
2249 | "Control"
2250 | "Fn"
2251 | "FnLock"
2252 | "Hyper"
2253 | "Meta"
2254 | "NumLock"
2255 | "ScrollLock"
2256 | "Shift"
2257 | "Super"
2258 | "Symbol"
2259 | "SymbolLock";
2260
2261 interface KeyboardEvent<T = Element> extends UIEvent<T, NativeKeyboardEvent> {
2262 altKey: boolean;
2263 /** @deprecated */
2264 charCode: number;
2265 ctrlKey: boolean;
2266 code: string;
2267 /**
2268 * See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
2269 */
2270 getModifierState(key: ModifierKey): boolean;
2271 /**
2272 * See the [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#named-key-attribute-values). for possible values
2273 */
2274 key: string;
2275 /** @deprecated */
2276 keyCode: number;
2277 locale: string;
2278 location: number;
2279 metaKey: boolean;
2280 repeat: boolean;
2281 shiftKey: boolean;
2282 /** @deprecated */
2283 which: number;
2284 }
2285
2286 interface MouseEvent<T = Element, E = NativeMouseEvent> extends UIEvent<T, E> {
2287 altKey: boolean;
2288 button: number;
2289 buttons: number;
2290 clientX: number;
2291 clientY: number;
2292 ctrlKey: boolean;
2293 /**
2294 * See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
2295 */
2296 getModifierState(key: ModifierKey): boolean;
2297 metaKey: boolean;
2298 movementX: number;
2299 movementY: number;
2300 pageX: number;
2301 pageY: number;
2302 relatedTarget: EventTarget | null;
2303 screenX: number;
2304 screenY: number;
2305 shiftKey: boolean;
2306 }
2307
2308 interface TouchEvent<T = Element> extends UIEvent<T, NativeTouchEvent> {
2309 altKey: boolean;
2310 changedTouches: TouchList;
2311 ctrlKey: boolean;
2312 /**
2313 * See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
2314 */
2315 getModifierState(key: ModifierKey): boolean;
2316 metaKey: boolean;
2317 shiftKey: boolean;
2318 targetTouches: TouchList;
2319 touches: TouchList;
2320 }
2321
2322 interface UIEvent<T = Element, E = NativeUIEvent> extends SyntheticEvent<T, E> {
2323 detail: number;
2324 view: AbstractView;
2325 }
2326
2327 interface WheelEvent<T = Element> extends MouseEvent<T, NativeWheelEvent> {
2328 deltaMode: number;
2329 deltaX: number;
2330 deltaY: number;
2331 deltaZ: number;
2332 }
2333
2334 interface AnimationEvent<T = Element> extends SyntheticEvent<T, NativeAnimationEvent> {
2335 animationName: string;
2336 elapsedTime: number;
2337 pseudoElement: string;
2338 }
2339
2340 interface TransitionEvent<T = Element> extends SyntheticEvent<T, NativeTransitionEvent> {
2341 elapsedTime: number;
2342 propertyName: string;
2343 pseudoElement: string;
2344 }
2345
2346 //
2347 // Event Handler Types
2348 // ----------------------------------------------------------------------
2349
2350 type EventHandler<E extends SyntheticEvent<any>> = { bivarianceHack(event: E): void }["bivarianceHack"];
2351
2352 type ReactEventHandler<T = Element> = EventHandler<SyntheticEvent<T>>;
2353
2354 type ClipboardEventHandler<T = Element> = EventHandler<ClipboardEvent<T>>;
2355 type CompositionEventHandler<T = Element> = EventHandler<CompositionEvent<T>>;
2356 type DragEventHandler<T = Element> = EventHandler<DragEvent<T>>;
2357 type FocusEventHandler<T = Element> = EventHandler<FocusEvent<T>>;
2358 type FormEventHandler<T = Element> = EventHandler<FormEvent<T>>;
2359 type ChangeEventHandler<T = Element> = EventHandler<ChangeEvent<T>>;
2360 type KeyboardEventHandler<T = Element> = EventHandler<KeyboardEvent<T>>;
2361 type MouseEventHandler<T = Element> = EventHandler<MouseEvent<T>>;
2362 type TouchEventHandler<T = Element> = EventHandler<TouchEvent<T>>;
2363 type PointerEventHandler<T = Element> = EventHandler<PointerEvent<T>>;
2364 type UIEventHandler<T = Element> = EventHandler<UIEvent<T>>;
2365 type WheelEventHandler<T = Element> = EventHandler<WheelEvent<T>>;
2366 type AnimationEventHandler<T = Element> = EventHandler<AnimationEvent<T>>;
2367 type TransitionEventHandler<T = Element> = EventHandler<TransitionEvent<T>>;
2368
2369 //
2370 // Props / DOM Attributes
2371 // ----------------------------------------------------------------------
2372
2373 interface HTMLProps<T> extends AllHTMLAttributes<T>, ClassAttributes<T> {
2374 }
2375
2376 type DetailedHTMLProps<E extends HTMLAttributes<T>, T> = ClassAttributes<T> & E;
2377
2378 interface SVGProps<T> extends SVGAttributes<T>, ClassAttributes<T> {
2379 }
2380
2381 interface SVGLineElementAttributes<T> extends SVGProps<T> {}
2382 interface SVGTextElementAttributes<T> extends SVGProps<T> {}
2383
2384 interface DOMAttributes<T> {
2385 children?: ReactNode | undefined;
2386 dangerouslySetInnerHTML?: {
2387 // Should be InnerHTML['innerHTML'].
2388 // But unfortunately we're mixing renderer-specific type declarations.
2389 __html: string | TrustedHTML;
2390 } | undefined;
2391
2392 // Clipboard Events
2393 onCopy?: ClipboardEventHandler<T> | undefined;
2394 onCopyCapture?: ClipboardEventHandler<T> | undefined;
2395 onCut?: ClipboardEventHandler<T> | undefined;
2396 onCutCapture?: ClipboardEventHandler<T> | undefined;
2397 onPaste?: ClipboardEventHandler<T> | undefined;
2398 onPasteCapture?: ClipboardEventHandler<T> | undefined;
2399
2400 // Composition Events
2401 onCompositionEnd?: CompositionEventHandler<T> | undefined;
2402 onCompositionEndCapture?: CompositionEventHandler<T> | undefined;
2403 onCompositionStart?: CompositionEventHandler<T> | undefined;
2404 onCompositionStartCapture?: CompositionEventHandler<T> | undefined;
2405 onCompositionUpdate?: CompositionEventHandler<T> | undefined;
2406 onCompositionUpdateCapture?: CompositionEventHandler<T> | undefined;
2407
2408 // Focus Events
2409 onFocus?: FocusEventHandler<T> | undefined;
2410 onFocusCapture?: FocusEventHandler<T> | undefined;
2411 onBlur?: FocusEventHandler<T> | undefined;
2412 onBlurCapture?: FocusEventHandler<T> | undefined;
2413
2414 // Form Events
2415 onChange?: FormEventHandler<T> | undefined;
2416 onChangeCapture?: FormEventHandler<T> | undefined;
2417 onBeforeInput?: FormEventHandler<T> | undefined;
2418 onBeforeInputCapture?: FormEventHandler<T> | undefined;
2419 onInput?: FormEventHandler<T> | undefined;
2420 onInputCapture?: FormEventHandler<T> | undefined;
2421 onReset?: FormEventHandler<T> | undefined;
2422 onResetCapture?: FormEventHandler<T> | undefined;
2423 onSubmit?: FormEventHandler<T> | undefined;
2424 onSubmitCapture?: FormEventHandler<T> | undefined;
2425 onInvalid?: FormEventHandler<T> | undefined;
2426 onInvalidCapture?: FormEventHandler<T> | undefined;
2427
2428 // Image Events
2429 onLoad?: ReactEventHandler<T> | undefined;
2430 onLoadCapture?: ReactEventHandler<T> | undefined;
2431 onError?: ReactEventHandler<T> | undefined; // also a Media Event
2432 onErrorCapture?: ReactEventHandler<T> | undefined; // also a Media Event
2433
2434 // Keyboard Events
2435 onKeyDown?: KeyboardEventHandler<T> | undefined;
2436 onKeyDownCapture?: KeyboardEventHandler<T> | undefined;
2437 /** @deprecated Use `onKeyUp` or `onKeyDown` instead */
2438 onKeyPress?: KeyboardEventHandler<T> | undefined;
2439 /** @deprecated Use `onKeyUpCapture` or `onKeyDownCapture` instead */
2440 onKeyPressCapture?: KeyboardEventHandler<T> | undefined;
2441 onKeyUp?: KeyboardEventHandler<T> | undefined;
2442 onKeyUpCapture?: KeyboardEventHandler<T> | undefined;
2443
2444 // Media Events
2445 onAbort?: ReactEventHandler<T> | undefined;
2446 onAbortCapture?: ReactEventHandler<T> | undefined;
2447 onCanPlay?: ReactEventHandler<T> | undefined;
2448 onCanPlayCapture?: ReactEventHandler<T> | undefined;
2449 onCanPlayThrough?: ReactEventHandler<T> | undefined;
2450 onCanPlayThroughCapture?: ReactEventHandler<T> | undefined;
2451 onDurationChange?: ReactEventHandler<T> | undefined;
2452 onDurationChangeCapture?: ReactEventHandler<T> | undefined;
2453 onEmptied?: ReactEventHandler<T> | undefined;
2454 onEmptiedCapture?: ReactEventHandler<T> | undefined;
2455 onEncrypted?: ReactEventHandler<T> | undefined;
2456 onEncryptedCapture?: ReactEventHandler<T> | undefined;
2457 onEnded?: ReactEventHandler<T> | undefined;
2458 onEndedCapture?: ReactEventHandler<T> | undefined;
2459 onLoadedData?: ReactEventHandler<T> | undefined;
2460 onLoadedDataCapture?: ReactEventHandler<T> | undefined;
2461 onLoadedMetadata?: ReactEventHandler<T> | undefined;
2462 onLoadedMetadataCapture?: ReactEventHandler<T> | undefined;
2463 onLoadStart?: ReactEventHandler<T> | undefined;
2464 onLoadStartCapture?: ReactEventHandler<T> | undefined;
2465 onPause?: ReactEventHandler<T> | undefined;
2466 onPauseCapture?: ReactEventHandler<T> | undefined;
2467 onPlay?: ReactEventHandler<T> | undefined;
2468 onPlayCapture?: ReactEventHandler<T> | undefined;
2469 onPlaying?: ReactEventHandler<T> | undefined;
2470 onPlayingCapture?: ReactEventHandler<T> | undefined;
2471 onProgress?: ReactEventHandler<T> | undefined;
2472 onProgressCapture?: ReactEventHandler<T> | undefined;
2473 onRateChange?: ReactEventHandler<T> | undefined;
2474 onRateChangeCapture?: ReactEventHandler<T> | undefined;
2475 onResize?: ReactEventHandler<T> | undefined;
2476 onResizeCapture?: ReactEventHandler<T> | undefined;
2477 onSeeked?: ReactEventHandler<T> | undefined;
2478 onSeekedCapture?: ReactEventHandler<T> | undefined;
2479 onSeeking?: ReactEventHandler<T> | undefined;
2480 onSeekingCapture?: ReactEventHandler<T> | undefined;
2481 onStalled?: ReactEventHandler<T> | undefined;
2482 onStalledCapture?: ReactEventHandler<T> | undefined;
2483 onSuspend?: ReactEventHandler<T> | undefined;
2484 onSuspendCapture?: ReactEventHandler<T> | undefined;
2485 onTimeUpdate?: ReactEventHandler<T> | undefined;
2486 onTimeUpdateCapture?: ReactEventHandler<T> | undefined;
2487 onVolumeChange?: ReactEventHandler<T> | undefined;
2488 onVolumeChangeCapture?: ReactEventHandler<T> | undefined;
2489 onWaiting?: ReactEventHandler<T> | undefined;
2490 onWaitingCapture?: ReactEventHandler<T> | undefined;
2491
2492 // MouseEvents
2493 onAuxClick?: MouseEventHandler<T> | undefined;
2494 onAuxClickCapture?: MouseEventHandler<T> | undefined;
2495 onClick?: MouseEventHandler<T> | undefined;
2496 onClickCapture?: MouseEventHandler<T> | undefined;
2497 onContextMenu?: MouseEventHandler<T> | undefined;
2498 onContextMenuCapture?: MouseEventHandler<T> | undefined;
2499 onDoubleClick?: MouseEventHandler<T> | undefined;
2500 onDoubleClickCapture?: MouseEventHandler<T> | undefined;
2501 onDrag?: DragEventHandler<T> | undefined;
2502 onDragCapture?: DragEventHandler<T> | undefined;
2503 onDragEnd?: DragEventHandler<T> | undefined;
2504 onDragEndCapture?: DragEventHandler<T> | undefined;
2505 onDragEnter?: DragEventHandler<T> | undefined;
2506 onDragEnterCapture?: DragEventHandler<T> | undefined;
2507 onDragExit?: DragEventHandler<T> | undefined;
2508 onDragExitCapture?: DragEventHandler<T> | undefined;
2509 onDragLeave?: DragEventHandler<T> | undefined;
2510 onDragLeaveCapture?: DragEventHandler<T> | undefined;
2511 onDragOver?: DragEventHandler<T> | undefined;
2512 onDragOverCapture?: DragEventHandler<T> | undefined;
2513 onDragStart?: DragEventHandler<T> | undefined;
2514 onDragStartCapture?: DragEventHandler<T> | undefined;
2515 onDrop?: DragEventHandler<T> | undefined;
2516 onDropCapture?: DragEventHandler<T> | undefined;
2517 onMouseDown?: MouseEventHandler<T> | undefined;
2518 onMouseDownCapture?: MouseEventHandler<T> | undefined;
2519 onMouseEnter?: MouseEventHandler<T> | undefined;
2520 onMouseLeave?: MouseEventHandler<T> | undefined;
2521 onMouseMove?: MouseEventHandler<T> | undefined;
2522 onMouseMoveCapture?: MouseEventHandler<T> | undefined;
2523 onMouseOut?: MouseEventHandler<T> | undefined;
2524 onMouseOutCapture?: MouseEventHandler<T> | undefined;
2525 onMouseOver?: MouseEventHandler<T> | undefined;
2526 onMouseOverCapture?: MouseEventHandler<T> | undefined;
2527 onMouseUp?: MouseEventHandler<T> | undefined;
2528 onMouseUpCapture?: MouseEventHandler<T> | undefined;
2529
2530 // Selection Events
2531 onSelect?: ReactEventHandler<T> | undefined;
2532 onSelectCapture?: ReactEventHandler<T> | undefined;
2533
2534 // Touch Events
2535 onTouchCancel?: TouchEventHandler<T> | undefined;
2536 onTouchCancelCapture?: TouchEventHandler<T> | undefined;
2537 onTouchEnd?: TouchEventHandler<T> | undefined;
2538 onTouchEndCapture?: TouchEventHandler<T> | undefined;
2539 onTouchMove?: TouchEventHandler<T> | undefined;
2540 onTouchMoveCapture?: TouchEventHandler<T> | undefined;
2541 onTouchStart?: TouchEventHandler<T> | undefined;
2542 onTouchStartCapture?: TouchEventHandler<T> | undefined;
2543
2544 // Pointer Events
2545 onPointerDown?: PointerEventHandler<T> | undefined;
2546 onPointerDownCapture?: PointerEventHandler<T> | undefined;
2547 onPointerMove?: PointerEventHandler<T> | undefined;
2548 onPointerMoveCapture?: PointerEventHandler<T> | undefined;
2549 onPointerUp?: PointerEventHandler<T> | undefined;
2550 onPointerUpCapture?: PointerEventHandler<T> | undefined;
2551 onPointerCancel?: PointerEventHandler<T> | undefined;
2552 onPointerCancelCapture?: PointerEventHandler<T> | undefined;
2553 onPointerEnter?: PointerEventHandler<T> | undefined;
2554 onPointerLeave?: PointerEventHandler<T> | undefined;
2555 onPointerOver?: PointerEventHandler<T> | undefined;
2556 onPointerOverCapture?: PointerEventHandler<T> | undefined;
2557 onPointerOut?: PointerEventHandler<T> | undefined;
2558 onPointerOutCapture?: PointerEventHandler<T> | undefined;
2559 onGotPointerCapture?: PointerEventHandler<T> | undefined;
2560 onGotPointerCaptureCapture?: PointerEventHandler<T> | undefined;
2561 onLostPointerCapture?: PointerEventHandler<T> | undefined;
2562 onLostPointerCaptureCapture?: PointerEventHandler<T> | undefined;
2563
2564 // UI Events
2565 onScroll?: UIEventHandler<T> | undefined;
2566 onScrollCapture?: UIEventHandler<T> | undefined;
2567
2568 // Wheel Events
2569 onWheel?: WheelEventHandler<T> | undefined;
2570 onWheelCapture?: WheelEventHandler<T> | undefined;
2571
2572 // Animation Events
2573 onAnimationStart?: AnimationEventHandler<T> | undefined;
2574 onAnimationStartCapture?: AnimationEventHandler<T> | undefined;
2575 onAnimationEnd?: AnimationEventHandler<T> | undefined;
2576 onAnimationEndCapture?: AnimationEventHandler<T> | undefined;
2577 onAnimationIteration?: AnimationEventHandler<T> | undefined;
2578 onAnimationIterationCapture?: AnimationEventHandler<T> | undefined;
2579
2580 // Transition Events
2581 onTransitionEnd?: TransitionEventHandler<T> | undefined;
2582 onTransitionEndCapture?: TransitionEventHandler<T> | undefined;
2583 }
2584
2585 export interface CSSProperties extends CSS.Properties<string | number> {
2586 /**
2587 * The index signature was removed to enable closed typing for style
2588 * using CSSType. You're able to use type assertion or module augmentation
2589 * to add properties or an index signature of your own.
2590 *
2591 * For examples and more information, visit:
2592 * https://github.com/frenic/csstype#what-should-i-do-when-i-get-type-errors
2593 */
2594 }
2595
2596 // All the WAI-ARIA 1.1 attributes from https://www.w3.org/TR/wai-aria-1.1/
2597 interface AriaAttributes {
2598 /** Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application. */
2599 "aria-activedescendant"?: string | undefined;
2600 /** Indicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by the aria-relevant attribute. */
2601 "aria-atomic"?: Booleanish | undefined;
2602 /**
2603 * Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be
2604 * presented if they are made.
2605 */
2606 "aria-autocomplete"?: "none" | "inline" | "list" | "both" | undefined;
2607 /** Indicates an element is being modified and that assistive technologies MAY want to wait until the modifications are complete before exposing them to the user. */
2608 /**
2609 * Defines a string value that labels the current element, which is intended to be converted into Braille.
2610 * @see aria-label.
2611 */
2612 "aria-braillelabel"?: string | undefined;
2613 /**
2614 * Defines a human-readable, author-localized abbreviated description for the role of an element, which is intended to be converted into Braille.
2615 * @see aria-roledescription.
2616 */
2617 "aria-brailleroledescription"?: string | undefined;
2618 "aria-busy"?: Booleanish | undefined;
2619 /**
2620 * Indicates the current "checked" state of checkboxes, radio buttons, and other widgets.
2621 * @see aria-pressed @see aria-selected.
2622 */
2623 "aria-checked"?: boolean | "false" | "mixed" | "true" | undefined;
2624 /**
2625 * Defines the total number of columns in a table, grid, or treegrid.
2626 * @see aria-colindex.
2627 */
2628 "aria-colcount"?: number | undefined;
2629 /**
2630 * Defines an element's column index or position with respect to the total number of columns within a table, grid, or treegrid.
2631 * @see aria-colcount @see aria-colspan.
2632 */
2633 "aria-colindex"?: number | undefined;
2634 /**
2635 * Defines a human readable text alternative of aria-colindex.
2636 * @see aria-rowindextext.
2637 */
2638 "aria-colindextext"?: string | undefined;
2639 /**
2640 * Defines the number of columns spanned by a cell or gridcell within a table, grid, or treegrid.
2641 * @see aria-colindex @see aria-rowspan.
2642 */
2643 "aria-colspan"?: number | undefined;
2644 /**
2645 * Identifies the element (or elements) whose contents or presence are controlled by the current element.
2646 * @see aria-owns.
2647 */
2648 "aria-controls"?: string | undefined;
2649 /** Indicates the element that represents the current item within a container or set of related elements. */
2650 "aria-current"?: boolean | "false" | "true" | "page" | "step" | "location" | "date" | "time" | undefined;
2651 /**
2652 * Identifies the element (or elements) that describes the object.
2653 * @see aria-labelledby
2654 */
2655 "aria-describedby"?: string | undefined;
2656 /**
2657 * Defines a string value that describes or annotates the current element.
2658 * @see related aria-describedby.
2659 */
2660 "aria-description"?: string | undefined;
2661 /**
2662 * Identifies the element that provides a detailed, extended description for the object.
2663 * @see aria-describedby.
2664 */
2665 "aria-details"?: string | undefined;
2666 /**
2667 * Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.
2668 * @see aria-hidden @see aria-readonly.
2669 */
2670 "aria-disabled"?: Booleanish | undefined;
2671 /**
2672 * Indicates what functions can be performed when a dragged object is released on the drop target.
2673 * @deprecated in ARIA 1.1
2674 */
2675 "aria-dropeffect"?: "none" | "copy" | "execute" | "link" | "move" | "popup" | undefined;
2676 /**
2677 * Identifies the element that provides an error message for the object.
2678 * @see aria-invalid @see aria-describedby.
2679 */
2680 "aria-errormessage"?: string | undefined;
2681 /** Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. */
2682 "aria-expanded"?: Booleanish | undefined;
2683 /**
2684 * Identifies the next element (or elements) in an alternate reading order of content which, at the user's discretion,
2685 * allows assistive technology to override the general default of reading in document source order.
2686 */
2687 "aria-flowto"?: string | undefined;
2688 /**
2689 * Indicates an element's "grabbed" state in a drag-and-drop operation.
2690 * @deprecated in ARIA 1.1
2691 */
2692 "aria-grabbed"?: Booleanish | undefined;
2693 /** Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. */
2694 "aria-haspopup"?: boolean | "false" | "true" | "menu" | "listbox" | "tre