-
Notifications
You must be signed in to change notification settings - Fork 30k
/
index.d.ts
4537 lines (4250 loc) · 189 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NOTE: Users of the `experimental` builds of React should add a reference
// to 'react/experimental' in their project. See experimental.d.ts's top comment
// for reference and documentation on how exactly to do it.
/// <reference path="global.d.ts" />
import * as CSS from "csstype";
import * as PropTypes from "prop-types";
type NativeAnimationEvent = AnimationEvent;
type NativeClipboardEvent = ClipboardEvent;
type NativeCompositionEvent = CompositionEvent;
type NativeDragEvent = DragEvent;
type NativeFocusEvent = FocusEvent;
type NativeKeyboardEvent = KeyboardEvent;
type NativeMouseEvent = MouseEvent;
type NativeTouchEvent = TouchEvent;
type NativePointerEvent = PointerEvent;
type NativeTransitionEvent = TransitionEvent;
type NativeUIEvent = UIEvent;
type NativeWheelEvent = WheelEvent;
/**
* Used to represent DOM API's where users can either pass
* true or false as a boolean or as its equivalent strings.
*/
type Booleanish = boolean | "true" | "false";
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin MDN}
*/
type CrossOrigin = "anonymous" | "use-credentials" | "" | undefined;
declare const UNDEFINED_VOID_ONLY: unique symbol;
/**
* The function returned from an effect passed to {@link React.useEffect useEffect},
* which can be used to clean up the effect when the component unmounts.
*
* @see {@link https://react.dev/reference/react/useEffect React Docs}
*/
type Destructor = () => void | { [UNDEFINED_VOID_ONLY]: never };
type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
// eslint-disable-next-line @definitelytyped/export-just-namespace
export = React;
export as namespace React;
declare namespace React {
//
// React Elements
// ----------------------------------------------------------------------
/**
* Used to retrieve the possible components which accept a given set of props.
*
* Can be passed no type parameters to get a union of all possible components
* and tags.
*
* Is a superset of {@link ComponentType}.
*
* @template P The props to match against. If not passed, defaults to any.
* @template Tag An optional tag to match against. If not passed, attempts to match against all possible tags.
*
* @example
*
* ```tsx
* // All components and tags (img, embed etc.)
* // which accept `src`
* type SrcComponents = ElementType<{ src: any }>;
* ```
*
* @example
*
* ```tsx
* // All components
* type AllComponents = ElementType;
* ```
*
* @example
*
* ```tsx
* // All custom components which match `src`, and tags which
* // match `src`, narrowed down to just `audio` and `embed`
* type SrcComponents = ElementType<{ src: any }, 'audio' | 'embed'>;
* ```
*/
type ElementType<P = any, Tag extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements> =
| { [K in Tag]: P extends JSX.IntrinsicElements[K] ? K : never }[Tag]
| ComponentType<P>;
/**
* Represents any user-defined component, either as a function or a class.
*
* Similar to {@link JSXElementConstructor}, but with extra properties like
* {@link FunctionComponent.defaultProps defaultProps } and
* {@link ComponentClass.contextTypes contextTypes}.
*
* @template P The props the component accepts.
*
* @see {@link ComponentClass}
* @see {@link FunctionComponent}
*/
type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
/**
* Represents any user-defined component, either as a function or a class.
*
* Similar to {@link ComponentType}, but without extra properties like
* {@link FunctionComponent.defaultProps defaultProps } and
* {@link ComponentClass.contextTypes contextTypes}.
*
* @template P The props the component accepts.
*/
type JSXElementConstructor<P> =
| ((
props: P,
/**
* @deprecated
*
* @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-stateless-function-components React Docs}
*/
deprecatedLegacyContext?: any,
) => ReactNode)
| (new(
props: P,
/**
* @deprecated
*
* @see {@link https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods React Docs}
*/
deprecatedLegacyContext?: any,
) => Component<any, any>);
/**
* A readonly ref container where {@link current} cannot be mutated.
*
* Created by {@link createRef}, or {@link useRef} when passed `null`.
*
* @template T The type of the ref's value.
*
* @example
*
* ```tsx
* const ref = createRef<HTMLDivElement>();
*
* ref.current = document.createElement('div'); // Error
* ```
*/
interface RefObject<T> {
/**
* The current value of the ref.
*/
readonly current: T | null;
}
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES {
}
/**
* A callback fired whenever the ref's value changes.
*
* @template T The type of the ref's value.
*
* @see {@link https://react.dev/reference/react-dom/components/common#ref-callback React Docs}
*
* @example
*
* ```tsx
* <div ref={(node) => console.log(node)} />
* ```
*/
type RefCallback<T> = {
bivarianceHack(
instance: T | null,
):
| void
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES
];
}["bivarianceHack"];
/**
* A union type of all possible shapes for React refs.
*
* @see {@link RefCallback}
* @see {@link RefObject}
*/
type Ref<T> = RefCallback<T> | RefObject<T> | null;
/**
* A legacy implementation of refs where you can pass a string to a ref prop.
*
* @see {@link https://react.dev/reference/react/Component#refs React Docs}
*
* @example
*
* ```tsx
* <div ref="myRef" />
* ```
*/
// TODO: Remove the string ref special case from `PropsWithRef` once we remove LegacyRef
type LegacyRef<T> = string | Ref<T>;
/**
* Retrieves the type of the 'ref' prop for a given component type or tag name.
*
* @template C The component type.
*
* @example
*
* ```tsx
* type MyComponentRef = React.ElementRef<typeof MyComponent>;
* ```
*
* @example
*
* ```tsx
* type DivRef = React.ElementRef<'div'>;
* ```
*/
type ElementRef<
C extends
| ForwardRefExoticComponent<any>
| { new(props: any): Component<any> }
| ((props: any, deprecatedLegacyContext?: any) => ReactNode)
| keyof JSX.IntrinsicElements,
> =
// need to check first if `ref` is a valid prop for ts@3.0
// otherwise it will infer `{}` instead of `never`
"ref" extends keyof ComponentPropsWithRef<C>
? NonNullable<ComponentPropsWithRef<C>["ref"]> extends RefAttributes<
infer Instance
>["ref"] ? Instance
: never
: never;
type ComponentState = any;
/**
* A value which uniquely identifies a node among items in an array.
*
* @see {@link https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key React Docs}
*/
type Key = string | number | bigint;
/**
* @internal The props any component can receive.
* You don't have to add this type. All components automatically accept these props.
* ```tsx
* const Component = () => <div />;
* <Component key="one" />
* ```
*
* WARNING: The implementation of a component will never have access to these attributes.
* The following example would be incorrect usage because {@link Component} would never have access to `key`:
* ```tsx
* const Component = (props: React.Attributes) => props.key;
* ```
*/
interface Attributes {
key?: Key | null | undefined;
}
/**
* The props any component accepting refs can receive.
* Class components, built-in browser components (e.g. `div`) and forwardRef components can receive refs and automatically accept these props.
* ```tsx
* const Component = forwardRef(() => <div />);
* <Component ref={(current) => console.log(current)} />
* ```
*
* You only need this type if you manually author the types of props that need to be compatible with legacy refs.
* ```tsx
* interface Props extends React.RefAttributes<HTMLDivElement> {}
* declare const Component: React.FunctionComponent<Props>;
* ```
*
* Otherwise it's simpler to directly use {@link Ref} since you can safely use the
* props type to describe to props that a consumer can pass to the component
* as well as describing the props the implementation of a component "sees".
* {@link RefAttributes} is generally not safe to describe both consumer and seen props.
*
* ```tsx
* interface Props extends {
* ref?: React.Ref<HTMLDivElement> | undefined;
* }
* declare const Component: React.FunctionComponent<Props>;
* ```
*
* WARNING: The implementation of a component will not have access to the same type in versions of React supporting string refs.
* The following example would be incorrect usage because {@link Component} would never have access to a `ref` with type `string`
* ```tsx
* const Component = (props: React.RefAttributes) => props.ref;
* ```
*/
interface RefAttributes<T> extends Attributes {
/**
* Allows getting a ref to the component instance.
* Once the component unmounts, React will set `ref.current` to `null`
* (or call the ref with `null` if you passed a callback ref).
*
* @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
*/
ref?: LegacyRef<T> | undefined;
}
/**
* Represents the built-in attributes available to class components.
*/
interface ClassAttributes<T> extends RefAttributes<T> {
}
/**
* Represents a JSX element.
*
* Where {@link ReactNode} represents everything that can be rendered, `ReactElement`
* only represents JSX.
*
* @template P The type of the props object
* @template T The type of the component or tag
*
* @example
*
* ```tsx
* const element: ReactElement = <div />;
* ```
*/
interface ReactElement<
P = any,
T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>,
> {
type: T;
props: P;
key: string | null;
}
/**
* @deprecated
*/
interface ReactComponentElement<
T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
P = Pick<ComponentProps<T>, Exclude<keyof ComponentProps<T>, "key" | "ref">>,
> extends ReactElement<P, Exclude<T, number>> {}
interface FunctionComponentElement<P> extends ReactElement<P, FunctionComponent<P>> {
ref?: ("ref" extends keyof P ? P extends { ref?: infer R | undefined } ? R : never : never) | undefined;
}
type CElement<P, T extends Component<P, ComponentState>> = ComponentElement<P, T>;
interface ComponentElement<P, T extends Component<P, ComponentState>> extends ReactElement<P, ComponentClass<P>> {
ref?: LegacyRef<T> | undefined;
}
/**
* @deprecated Use {@link ComponentElement} instead.
*/
type ClassicElement<P> = CElement<P, ClassicComponent<P, ComponentState>>;
// string fallback for custom web-components
interface DOMElement<P extends HTMLAttributes<T> | SVGAttributes<T>, T extends Element>
extends ReactElement<P, string>
{
ref: LegacyRef<T>;
}
// ReactHTML for ReactHTMLElement
interface ReactHTMLElement<T extends HTMLElement> extends DetailedReactHTMLElement<AllHTMLAttributes<T>, T> {}
interface DetailedReactHTMLElement<P extends HTMLAttributes<T>, T extends HTMLElement> extends DOMElement<P, T> {
type: keyof ReactHTML;
}
// ReactSVG for ReactSVGElement
interface ReactSVGElement extends DOMElement<SVGAttributes<SVGElement>, SVGElement> {
type: keyof ReactSVG;
}
interface ReactPortal extends ReactElement {
children: ReactNode;
}
//
// Factories
// ----------------------------------------------------------------------
type Factory<P> = (props?: Attributes & P, ...children: ReactNode[]) => ReactElement<P>;
/**
* @deprecated Please use `FunctionComponentFactory`
*/
type SFCFactory<P> = FunctionComponentFactory<P>;
type FunctionComponentFactory<P> = (
props?: Attributes & P,
...children: ReactNode[]
) => FunctionComponentElement<P>;
type ComponentFactory<P, T extends Component<P, ComponentState>> = (
props?: ClassAttributes<T> & P,
...children: ReactNode[]
) => CElement<P, T>;
type CFactory<P, T extends Component<P, ComponentState>> = ComponentFactory<P, T>;
type ClassicFactory<P> = CFactory<P, ClassicComponent<P, ComponentState>>;
type DOMFactory<P extends DOMAttributes<T>, T extends Element> = (
props?: ClassAttributes<T> & P | null,
...children: ReactNode[]
) => DOMElement<P, T>;
interface HTMLFactory<T extends HTMLElement> extends DetailedHTMLFactory<AllHTMLAttributes<T>, T> {}
interface DetailedHTMLFactory<P extends HTMLAttributes<T>, T extends HTMLElement> extends DOMFactory<P, T> {
(props?: ClassAttributes<T> & P | null, ...children: ReactNode[]): DetailedReactHTMLElement<P, T>;
}
interface SVGFactory extends DOMFactory<SVGAttributes<SVGElement>, SVGElement> {
(
props?: ClassAttributes<SVGElement> & SVGAttributes<SVGElement> | null,
...children: ReactNode[]
): ReactSVGElement;
}
/**
* @deprecated - This type is not relevant when using React. Inline the type instead to make the intent clear.
*/
type ReactText = string | number;
/**
* @deprecated - This type is not relevant when using React. Inline the type instead to make the intent clear.
*/
type ReactChild = ReactElement | string | number;
/**
* @deprecated Use either `ReactNode[]` if you need an array or `Iterable<ReactNode>` if its passed to a host component.
*/
interface ReactNodeArray extends ReadonlyArray<ReactNode> {}
/**
* WARNING: Not related to `React.Fragment`.
* @deprecated This type is not relevant when using React. Inline the type instead to make the intent clear.
*/
type ReactFragment = Iterable<ReactNode>;
/**
* Different release channels declare additional types of ReactNode this particular release channel accepts.
* App or library types should never augment this interface.
*/
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES {}
/**
* Represents all of the things React can render.
*
* Where {@link ReactElement} only represents JSX, `ReactNode` represents everything that can be rendered.
*
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/reactnode/ React TypeScript Cheatsheet}
*
* @example
*
* ```tsx
* // Typing children
* type Props = { children: ReactNode }
*
* const Component = ({ children }: Props) => <div>{children}</div>
*
* <Component>hello</Component>
* ```
*
* @example
*
* ```tsx
* // Typing a custom element
* type Props = { customElement: ReactNode }
*
* const Component = ({ customElement }: Props) => <div>{customElement}</div>
*
* <Component customElement={<div>hello</div>} />
* ```
*/
// non-thenables need to be kept in sync with AwaitedReactNode
type ReactNode =
| ReactElement
| string
| number
| Iterable<ReactNode>
| ReactPortal
| boolean
| null
| undefined
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
];
//
// Top Level API
// ----------------------------------------------------------------------
// DOM Elements
/** @deprecated */
function createFactory<T extends HTMLElement>(
type: keyof ReactHTML,
): HTMLFactory<T>;
/** @deprecated */
function createFactory(
type: keyof ReactSVG,
): SVGFactory;
/** @deprecated */
function createFactory<P extends DOMAttributes<T>, T extends Element>(
type: string,
): DOMFactory<P, T>;
// Custom components
/** @deprecated */
function createFactory<P>(type: FunctionComponent<P>): FunctionComponentFactory<P>;
/** @deprecated */
function createFactory<P, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(
type: ClassType<P, T, C>,
): CFactory<P, T>;
/** @deprecated */
function createFactory<P>(type: ComponentClass<P>): Factory<P>;
// DOM Elements
// TODO: generalize this to everything in `keyof ReactHTML`, not just "input"
function createElement(
type: "input",
props?: InputHTMLAttributes<HTMLInputElement> & ClassAttributes<HTMLInputElement> | null,
...children: ReactNode[]
): DetailedReactHTMLElement<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>;
function createElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
type: keyof ReactHTML,
props?: ClassAttributes<T> & P | null,
...children: ReactNode[]
): DetailedReactHTMLElement<P, T>;
function createElement<P extends SVGAttributes<T>, T extends SVGElement>(
type: keyof ReactSVG,
props?: ClassAttributes<T> & P | null,
...children: ReactNode[]
): ReactSVGElement;
function createElement<P extends DOMAttributes<T>, T extends Element>(
type: string,
props?: ClassAttributes<T> & P | null,
...children: ReactNode[]
): DOMElement<P, T>;
// Custom components
function createElement<P extends {}>(
type: FunctionComponent<P>,
props?: Attributes & P | null,
...children: ReactNode[]
): FunctionComponentElement<P>;
function createElement<P extends {}, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(
type: ClassType<P, T, C>,
props?: ClassAttributes<T> & P | null,
...children: ReactNode[]
): CElement<P, T>;
function createElement<P extends {}>(
type: FunctionComponent<P> | ComponentClass<P> | string,
props?: Attributes & P | null,
...children: ReactNode[]
): ReactElement<P>;
// DOM Elements
// ReactHTMLElement
function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
element: DetailedReactHTMLElement<P, T>,
props?: P,
...children: ReactNode[]
): DetailedReactHTMLElement<P, T>;
// ReactHTMLElement, less specific
function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
element: ReactHTMLElement<T>,
props?: P,
...children: ReactNode[]
): ReactHTMLElement<T>;
// SVGElement
function cloneElement<P extends SVGAttributes<T>, T extends SVGElement>(
element: ReactSVGElement,
props?: P,
...children: ReactNode[]
): ReactSVGElement;
// DOM Element (has to be the last, because type checking stops at first overload that fits)
function cloneElement<P extends DOMAttributes<T>, T extends Element>(
element: DOMElement<P, T>,
props?: DOMAttributes<T> & P,
...children: ReactNode[]
): DOMElement<P, T>;
// Custom components
function cloneElement<P>(
element: FunctionComponentElement<P>,
props?: Partial<P> & Attributes,
...children: ReactNode[]
): FunctionComponentElement<P>;
function cloneElement<P, T extends Component<P, ComponentState>>(
element: CElement<P, T>,
props?: Partial<P> & ClassAttributes<T>,
...children: ReactNode[]
): CElement<P, T>;
function cloneElement<P>(
element: ReactElement<P>,
props?: Partial<P> & Attributes,
...children: ReactNode[]
): ReactElement<P>;
/**
* Describes the props accepted by a Context {@link Provider}.
*
* @template T The type of the value the context provides.
*/
interface ProviderProps<T> {
value: T;
children?: ReactNode | undefined;
}
/**
* Describes the props accepted by a Context {@link Consumer}.
*
* @template T The type of the value the context provides.
*/
interface ConsumerProps<T> {
children: (value: T) => ReactNode;
}
/**
* An object masquerading as a component. These are created by functions
* like {@link forwardRef}, {@link memo}, and {@link createContext}.
*
* In order to make TypeScript work, we pretend that they are normal
* components.
*
* But they are, in fact, not callable - instead, they are objects which
* are treated specially by the renderer.
*
* @template P The props the component accepts.
*/
interface ExoticComponent<P = {}> {
(props: P): ReactNode;
readonly $$typeof: symbol;
}
/**
* An {@link ExoticComponent} with a `displayName` property applied to it.
*
* @template P The props the component accepts.
*/
interface NamedExoticComponent<P = {}> extends ExoticComponent<P> {
/**
* Used in debugging messages. You might want to set it
* explicitly if you want to display a different name for
* debugging purposes.
*
* @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
*/
displayName?: string | undefined;
}
/**
* An {@link ExoticComponent} with a `propTypes` property applied to it.
*
* @template P The props the component accepts.
*/
interface ProviderExoticComponent<P> extends ExoticComponent<P> {
propTypes?: WeakValidationMap<P> | undefined;
}
/**
* Used to retrieve the type of a context object from a {@link Context}.
*
* @template C The context object.
*
* @example
*
* ```tsx
* import { createContext } from 'react';
*
* const MyContext = createContext({ foo: 'bar' });
*
* type ContextType = ContextType<typeof MyContext>;
* // ContextType = { foo: string }
* ```
*/
type ContextType<C extends Context<any>> = C extends Context<infer T> ? T : never;
/**
* Wraps your components to specify the value of this context for all components inside.
*
* @see {@link https://react.dev/reference/react/createContext#provider React Docs}
*
* @example
*
* ```tsx
* import { createContext } from 'react';
*
* const ThemeContext = createContext('light');
*
* function App() {
* return (
* <ThemeContext.Provider value="dark">
* <Toolbar />
* </ThemeContext.Provider>
* );
* }
* ```
*/
type Provider<T> = ProviderExoticComponent<ProviderProps<T>>;
/**
* The old way to read context, before {@link useContext} existed.
*
* @see {@link https://react.dev/reference/react/createContext#consumer React Docs}
*
* @example
*
* ```tsx
* import { UserContext } from './user-context';
*
* function Avatar() {
* return (
* <UserContext.Consumer>
* {user => <img src={user.profileImage} alt={user.name} />}
* </UserContext.Consumer>
* );
* }
* ```
*/
type Consumer<T> = ExoticComponent<ConsumerProps<T>>;
/**
* Context lets components pass information deep down without explicitly
* passing props.
*
* Created from {@link createContext}
*
* @see {@link https://react.dev/learn/passing-data-deeply-with-context React Docs}
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/context/ React TypeScript Cheatsheet}
*
* @example
*
* ```tsx
* import { createContext } from 'react';
*
* const ThemeContext = createContext('light');
* ```
*/
interface Context<T> {
Provider: Provider<T>;
Consumer: Consumer<T>;
/**
* Used in debugging messages. You might want to set it
* explicitly if you want to display a different name for
* debugging purposes.
*
* @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
*/
displayName?: string | undefined;
}
/**
* Lets you create a {@link Context} that components can provide or read.
*
* @param defaultValue The value you want the context to have when there is no matching
* {@link Provider} in the tree above the component reading the context. This is meant
* as a "last resort" fallback.
*
* @see {@link https://react.dev/reference/react/createContext#reference React Docs}
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/context/ React TypeScript Cheatsheet}
*
* @example
*
* ```tsx
* import { createContext } from 'react';
*
* const ThemeContext = createContext('light');
* ```
*/
function createContext<T>(
// If you thought this should be optional, see
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/24509#issuecomment-382213106
defaultValue: T,
): Context<T>;
function isValidElement<P>(object: {} | null | undefined): object is ReactElement<P>;
/**
* Maintainer's note: Sync with {@link ReactChildren} until {@link ReactChildren} is removed.
*/
const Children: {
map<T, C>(
children: C | readonly C[],
fn: (child: C, index: number) => T,
): C extends null | undefined ? C : Array<Exclude<T, boolean | null | undefined>>;
forEach<C>(children: C | readonly C[], fn: (child: C, index: number) => void): void;
count(children: any): number;
only<C>(children: C): C extends any[] ? never : C;
toArray(children: ReactNode | ReactNode[]): Array<Exclude<ReactNode, boolean | null | undefined>>;
};
/**
* Lets you group elements without a wrapper node.
*
* @see {@link https://react.dev/reference/react/Fragment React Docs}
*
* @example
*
* ```tsx
* import { Fragment } from 'react';
*
* <Fragment>
* <td>Hello</td>
* <td>World</td>
* </Fragment>
* ```
*
* @example
*
* ```tsx
* // Using the <></> shorthand syntax:
*
* <>
* <td>Hello</td>
* <td>World</td>
* </>
* ```
*/
const Fragment: ExoticComponent<{ children?: ReactNode | undefined }>;
/**
* Lets you find common bugs in your components early during development.
*
* @see {@link https://react.dev/reference/react/StrictMode React Docs}
*
* @example
*
* ```tsx
* import { StrictMode } from 'react';
*
* <StrictMode>
* <App />
* </StrictMode>
* ```
*/
const StrictMode: ExoticComponent<{ children?: ReactNode | undefined }>;
/**
* The props accepted by {@link Suspense}.
*
* @see {@link https://react.dev/reference/react/Suspense React Docs}
*/
interface SuspenseProps {
children?: ReactNode | undefined;
/** A fallback react tree to show when a Suspense child (like React.lazy) suspends */
fallback?: ReactNode;
}
/**
* Lets you display a fallback until its children have finished loading.
*
* @see {@link https://react.dev/reference/react/Suspense React Docs}
*
* @example
*
* ```tsx
* import { Suspense } from 'react';
*
* <Suspense fallback={<Loading />}>
* <ProfileDetails />
* </Suspense>
* ```
*/
const Suspense: ExoticComponent<SuspenseProps>;
const version: string;
/**
* The callback passed to {@link ProfilerProps.onRender}.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
type ProfilerOnRenderCallback = (
/**
* The string id prop of the {@link Profiler} tree that has just committed. This lets
* you identify which part of the tree was committed if you are using multiple
* profilers.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
id: string,
/**
* This lets you know whether the tree has just been mounted for the first time
* or re-rendered due to a change in props, state, or hooks.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
phase: "mount" | "update" | "nested-update",
/**
* The number of milliseconds spent rendering the {@link Profiler} and its descendants
* for the current update. This indicates how well the subtree makes use of
* memoization (e.g. {@link memo} and {@link useMemo}). Ideally this value should decrease
* significantly after the initial mount as many of the descendants will only need to
* re-render if their specific props change.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
actualDuration: number,
/**
* The number of milliseconds estimating how much time it would take to re-render the entire
* {@link Profiler} subtree without any optimizations. It is calculated by summing up the most
* recent render durations of each component in the tree. This value estimates a worst-case
* cost of rendering (e.g. the initial mount or a tree with no memoization). Compare
* {@link actualDuration} against it to see if memoization is working.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
baseDuration: number,
/**
* A numeric timestamp for when React began rendering the current update.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
startTime: number,
/**
* A numeric timestamp for when React committed the current update. This value is shared
* between all profilers in a commit, enabling them to be grouped if desirable.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*/
commitTime: number,
) => void;
/**
* The props accepted by {@link Profiler}.
*
* @see {@link https://react.dev/reference/react/Profiler React Docs}
*/
interface ProfilerProps {
children?: ReactNode | undefined;
id: string;
onRender: ProfilerOnRenderCallback;
}
/**
* Lets you measure rendering performance of a React tree programmatically.
*
* @see {@link https://react.dev/reference/react/Profiler#onrender-callback React Docs}
*
* @example
*
* ```tsx
* <Profiler id="App" >
* <App />
* </Profiler>
* ```
*/
const Profiler: ExoticComponent<ProfilerProps>;
//
// Component API
// ----------------------------------------------------------------------
type ReactInstance = Component<any> | Element;
// Base component for plain JS classes
interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {}
class Component<P, S> {
/**
* If set, `this.context` will be set at runtime to the current value of the given Context.
*
* @example
*
* ```ts
* type MyContext = number
* const Ctx = React.createContext<MyContext>(0)
*
* class Foo extends React.Component {
* static contextType = Ctx
* context!: React.ContextType<typeof Ctx>
* render () {
* return <>My context's value: {this.context}</>;
* }
* }
* ```
*
* @see {@link https://react.dev/reference/react/Component#static-contexttype}
*/
static contextType?: Context<any> | undefined;
/**
* If using the new style context, re-declare this in your class to be the
* `React.ContextType` of your `static contextType`.
* Should be used with type annotation or static contextType.
*
* @example
* ```ts
* static contextType = MyContext
* // For TS pre-3.7:
* context!: React.ContextType<typeof MyContext>
* // For TS 3.7 and above:
* declare context: React.ContextType<typeof MyContext>
* ```
*
* @see {@link https://react.dev/reference/react/Component#context React Docs}
*/
context: unknown;