@types/react 18.2.53 → 18.2.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. react/README.md +1 -1
  2. react/index.d.ts +133 -61
  3. react/package.json +2 -2
  4. react/ts5.0/index.d.ts +107 -40
react/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for react (https://react.dev/).
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Mon, 05 Feb 2024 04:07:15 GMT
11
+ * Last updated: Mon, 05 Feb 2024 19:06:53 GMT
12
12
  * Dependencies: [@types/prop-types](https://npmjs.com/package/@types/prop-types), [@types/scheduler](https://npmjs.com/package/@types/scheduler), [csstype](https://npmjs.com/package/csstype)
13
13
 
14
14
  # Credits
react/index.d.ts CHANGED
@@ -91,8 +91,11 @@ declare namespace React {
91
91
  | ComponentType<P>;
92
92
 
93
93
  /**
94
- * Represents any user-defined component, either as a function component or
95
- * a class component.
94
+ * Represents any user-defined component, either as a function or a class.
95
+ *
96
+ * Similar to {@link JSXElementConstructor}, but with extra properties like
97
+ * {@link FunctionComponent.defaultProps defaultProps } and
98
+ * {@link ComponentClass.contextTypes contextTypes}.
96
99
  *
97
100
  * @template P The props the component accepts.
98
101
  *
@@ -101,6 +104,15 @@ declare namespace React {
101
104
  */
102
105
  type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
103
106
 
107
+ /**
108
+ * Represents any user-defined component, either as a function or a class.
109
+ *
110
+ * Similar to {@link ComponentType}, but without extra properties like
111
+ * {@link FunctionComponent.defaultProps defaultProps } and
112
+ * {@link ComponentClass.contextTypes contextTypes}.
113
+ *
114
+ * @template P The props the component accepts.
115
+ */
104
116
  type JSXElementConstructor<P> =
105
117
  | ((
106
118
  props: P,
@@ -137,6 +149,9 @@ declare namespace React {
137
149
  * ```
138
150
  */
139
151
  interface RefObject<T> {
152
+ /**
153
+ * The current value of the ref.
154
+ */
140
155
  readonly current: T | null;
141
156
  }
142
157
 
@@ -175,22 +190,23 @@ declare namespace React {
175
190
  * ```
176
191
  */
177
192
  type LegacyRef<T> = string | Ref<T>;
193
+
178
194
  /**
179
- * Gets the instance type for a React element. The instance will be different for various component types:
195
+ * Retrieves the type of the 'ref' prop for a given component type or tag name.
196
+ *
197
+ * @template C The component type.
180
198
  *
181
- * - React class components will be the class instance. So if you had `class Foo extends React.Component<{}> {}`
182
- * and used `React.ElementRef<typeof Foo>` then the type would be the instance of `Foo`.
183
- * - React stateless functional components do not have a backing instance and so `React.ElementRef<typeof Bar>`
184
- * (when `Bar` is `function Bar() {}`) will give you the `undefined` type.
185
- * - JSX intrinsics like `div` will give you their DOM instance. For `React.ElementRef<'div'>` that would be
186
- * `HTMLDivElement`. For `React.ElementRef<'input'>` that would be `HTMLInputElement`.
187
- * - React stateless functional components that forward a `ref` will give you the `ElementRef` of the forwarded
188
- * to component.
199
+ * @example
189
200
  *
190
- * `C` must be the type _of_ a React component so you need to use typeof as in `React.ElementRef<typeof MyComponent>`.
201
+ * ```tsx
202
+ * type MyComponentRef = React.ElementRef<typeof MyComponent>;
203
+ * ```
204
+ *
205
+ * @example
191
206
  *
192
- * @todo In Flow, this works a little different with forwarded refs and the `AbstractComponent` that
193
- * `React.forwardRef()` returns.
207
+ * ```tsx
208
+ * type DivRef = React.ElementRef<'div'>;
209
+ * ```
194
210
  */
195
211
  type ElementRef<
196
212
  C extends
@@ -216,26 +232,30 @@ declare namespace React {
216
232
  */
217
233
  type Key = string | number | bigint;
218
234
 
219
- /**
220
- * @internal You shouldn't need to use this type since you never see these attributes
221
- * inside your component or have to validate them.
222
- */
223
235
  interface Attributes {
224
236
  key?: Key | null | undefined;
225
237
  }
226
238
  interface RefAttributes<T> extends Attributes {
227
239
  /**
228
240
  * Allows getting a ref to the component instance.
229
- * Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref).
230
- * @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom}
241
+ * Once the component unmounts, React will set `ref.current` to `null`
242
+ * (or call the ref with `null` if you passed a callback ref).
243
+ *
244
+ * @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
231
245
  */
232
246
  ref?: Ref<T> | undefined;
233
247
  }
248
+
249
+ /**
250
+ * Represents the built-in attributes available to class components.
251
+ */
234
252
  interface ClassAttributes<T> extends Attributes {
235
253
  /**
236
254
  * Allows getting a ref to the component instance.
237
- * Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref).
238
- * @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom}
255
+ * Once the component unmounts, React will set `ref.current` to `null`
256
+ * (or call the ref with `null` if you passed a callback ref).
257
+ *
258
+ * @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
239
259
  */
240
260
  ref?: LegacyRef<T> | undefined;
241
261
  }
@@ -264,7 +284,7 @@ declare namespace React {
264
284
  }
265
285
 
266
286
  /**
267
- * @deprecated Use `ComponentElement<P, ClassicComponent<P, any>>` instead.
287
+ * @deprecated Use {@link ComponentElement} instead.
268
288
  */
269
289
  type ClassicElement<P> = CElement<P, ClassicComponent<P, ComponentState>>;
270
290
 
@@ -352,11 +372,38 @@ declare namespace React {
352
372
  type ReactFragment = Iterable<ReactNode>;
353
373
 
354
374
  /**
355
- * For internal usage only.
356
375
  * Different release channels declare additional types of ReactNode this particular release channel accepts.
357
376
  * App or library types should never augment this interface.
358
377
  */
359
378
  interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES {}
379
+
380
+ /**
381
+ * Represents all of the things React can render.
382
+ *
383
+ * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/reactnode/ React TypeScript Cheatsheet}
384
+ *
385
+ * @example
386
+ *
387
+ * ```tsx
388
+ * // Typing children
389
+ * type Props = { children: ReactNode }
390
+ *
391
+ * const Component = ({ children }: Props) => <div>{children}</div>
392
+ *
393
+ * <Component>hello</Component>
394
+ * ```
395
+ *
396
+ * @example
397
+ *
398
+ * ```tsx
399
+ * // Typing a custom element
400
+ * type Props = { customElement: ReactNode }
401
+ *
402
+ * const Component = ({ customElement }: Props) => <div>{customElement}</div>
403
+ *
404
+ * <Component customElement={<div>hello</div>} />
405
+ * ```
406
+ */
360
407
  type ReactNode =
361
408
  | ReactElement
362
409
  | string
@@ -476,12 +523,21 @@ declare namespace React {
476
523
  ...children: ReactNode[]
477
524
  ): ReactElement<P>;
478
525
 
479
- // Context via RenderProps
526
+ /**
527
+ * Describes the props accepted by a Context {@link Provider}.
528
+ *
529
+ * @template T The type of the value the context provides.
530
+ */
480
531
  interface ProviderProps<T> {
481
532
  value: T;
482
533
  children?: ReactNode | undefined;
483
534
  }
484
535
 
536
+ /**
537
+ * Describes the props accepted by a Context {@link Consumer}.
538
+ *
539
+ * @template T The type of the value the context provides.
540
+ */
485
541
  interface ConsumerProps<T> {
486
542
  children: (value: T) => ReactNode;
487
543
  }
@@ -636,7 +692,9 @@ declare namespace React {
636
692
 
637
693
  function isValidElement<P>(object: {} | null | undefined): object is ReactElement<P>;
638
694
 
639
- // Sync with `ReactChildren` until `ReactChildren` is removed.
695
+ /**
696
+ * Maintainer's note: Sync with {@link ReactChildren} until {@link ReactChildren} is removed.
697
+ */
640
698
  const Children: {
641
699
  map<T, C>(
642
700
  children: C | readonly C[],
@@ -815,11 +873,10 @@ declare namespace React {
815
873
  // Base component for plain JS classes
816
874
  interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {}
817
875
  class Component<P, S> {
818
- // tslint won't let me format the sample code in a way that vscode likes it :(
819
876
  /**
820
877
  * If set, `this.context` will be set at runtime to the current value of the given Context.
821
878
  *
822
- * Usage:
879
+ * @example
823
880
  *
824
881
  * ```ts
825
882
  * type MyContext = number
@@ -843,6 +900,7 @@ declare namespace React {
843
900
  * `React.ContextType` of your `static contextType`.
844
901
  * Should be used with type annotation or static contextType.
845
902
  *
903
+ * @example
846
904
  * ```ts
847
905
  * static contextType = MyContext
848
906
  * // For TS pre-3.7:
@@ -851,14 +909,14 @@ declare namespace React {
851
909
  * declare context: React.ContextType<typeof MyContext>
852
910
  * ```
853
911
  *
854
- * @see {@link https://react.dev/reference/react/Component#context}
912
+ * @see {@link https://react.dev/reference/react/Component#context React Docs}
855
913
  */
856
914
  context: unknown;
857
915
 
858
916
  constructor(props: Readonly<P> | P);
859
917
  /**
860
918
  * @deprecated
861
- * @see {@link https://legacy.reactjs.org/docs/legacy-context.html}
919
+ * @see {@link https://legacy.reactjs.org/docs/legacy-context.html React Docs}
862
920
  */
863
921
  constructor(props: P, context: any);
864
922
 
@@ -877,7 +935,8 @@ declare namespace React {
877
935
  state: Readonly<S>;
878
936
  /**
879
937
  * @deprecated
880
- * https://legacy.reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs
938
+ *
939
+ * @see {@link https://legacy.reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs Legacy React Docs}
881
940
  */
882
941
  refs: {
883
942
  [key: string]: ReactInstance;
@@ -1033,6 +1092,7 @@ declare namespace React {
1033
1092
  * @deprecated - Equivalent to {@link React.FunctionComponent}.
1034
1093
  *
1035
1094
  * @see {@link React.FunctionComponent}
1095
+ * @alias {@link VoidFunctionComponent}
1036
1096
  */
1037
1097
  type VFC<P = {}> = VoidFunctionComponent<P>;
1038
1098
 
@@ -1193,7 +1253,7 @@ declare namespace React {
1193
1253
  * `PureComponent` implements a shallow comparison on props and state and returns true if any
1194
1254
  * props or states have changed.
1195
1255
  *
1196
- * If false is returned, `Component#render`, `componentWillUpdate`
1256
+ * If false is returned, {@link Component.render}, `componentWillUpdate`
1197
1257
  * and `componentDidUpdate` will not be called.
1198
1258
  */
1199
1259
  shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
@@ -1235,45 +1295,47 @@ declare namespace React {
1235
1295
  // This should be "infer SS" but can't use it yet
1236
1296
  interface NewLifecycle<P, S, SS> {
1237
1297
  /**
1238
- * Runs before React applies the result of `render` to the document, and
1239
- * returns an object to be given to componentDidUpdate. Useful for saving
1240
- * things such as scroll position before `render` causes changes to it.
1298
+ * Runs before React applies the result of {@link Component.render render} to the document, and
1299
+ * returns an object to be given to {@link componentDidUpdate}. Useful for saving
1300
+ * things such as scroll position before {@link Component.render render} causes changes to it.
1241
1301
  *
1242
- * Note: the presence of getSnapshotBeforeUpdate prevents any of the deprecated
1302
+ * Note: the presence of this method prevents any of the deprecated
1243
1303
  * lifecycle events from running.
1244
1304
  */
1245
1305
  getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>): SS | null;
1246
1306
  /**
1247
1307
  * Called immediately after updating occurs. Not called for the initial render.
1248
1308
  *
1249
- * The snapshot is only present if getSnapshotBeforeUpdate is present and returns non-null.
1309
+ * The snapshot is only present if {@link getSnapshotBeforeUpdate} is present and returns non-null.
1250
1310
  */
1251
1311
  componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot?: SS): void;
1252
1312
  }
1253
1313
 
1254
1314
  interface DeprecatedLifecycle<P, S> {
1255
1315
  /**
1256
- * Called immediately before mounting occurs, and before `Component#render`.
1316
+ * Called immediately before mounting occurs, and before {@link Component.render}.
1257
1317
  * Avoid introducing any side-effects or subscriptions in this method.
1258
1318
  *
1259
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1260
- * prevents this from being invoked.
1319
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1320
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1321
+ * this from being invoked.
1261
1322
  *
1262
- * @deprecated 16.3, use componentDidMount or the constructor instead; will stop working in React 17
1323
+ * @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead; will stop working in React 17
1263
1324
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
1264
1325
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1265
1326
  */
1266
1327
  componentWillMount?(): void;
1267
1328
  /**
1268
- * Called immediately before mounting occurs, and before `Component#render`.
1329
+ * Called immediately before mounting occurs, and before {@link Component.render}.
1269
1330
  * Avoid introducing any side-effects or subscriptions in this method.
1270
1331
  *
1271
1332
  * This method will not stop working in React 17.
1272
1333
  *
1273
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1274
- * prevents this from being invoked.
1334
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1335
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1336
+ * this from being invoked.
1275
1337
  *
1276
- * @deprecated 16.3, use componentDidMount or the constructor instead
1338
+ * @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead
1277
1339
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
1278
1340
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1279
1341
  */
@@ -1283,12 +1345,13 @@ declare namespace React {
1283
1345
  * React may call this even if props have not changed, so be sure to compare new and existing
1284
1346
  * props if you only want to handle changes.
1285
1347
  *
1286
- * Calling `Component#setState` generally does not trigger this method.
1348
+ * Calling {@link Component.setState} generally does not trigger this method.
1287
1349
  *
1288
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1289
- * prevents this from being invoked.
1350
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1351
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1352
+ * this from being invoked.
1290
1353
  *
1291
- * @deprecated 16.3, use static getDerivedStateFromProps instead; will stop working in React 17
1354
+ * @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead; will stop working in React 17
1292
1355
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
1293
1356
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1294
1357
  */
@@ -1298,14 +1361,15 @@ declare namespace React {
1298
1361
  * React may call this even if props have not changed, so be sure to compare new and existing
1299
1362
  * props if you only want to handle changes.
1300
1363
  *
1301
- * Calling `Component#setState` generally does not trigger this method.
1364
+ * Calling {@link Component.setState} generally does not trigger this method.
1302
1365
  *
1303
1366
  * This method will not stop working in React 17.
1304
1367
  *
1305
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1306
- * prevents this from being invoked.
1368
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1369
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1370
+ * this from being invoked.
1307
1371
  *
1308
- * @deprecated 16.3, use static getDerivedStateFromProps instead
1372
+ * @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead
1309
1373
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
1310
1374
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
1311
1375
  */
@@ -1313,10 +1377,11 @@ declare namespace React {
1313
1377
  /**
1314
1378
  * Called immediately before rendering when new props or state is received. Not called for the initial render.
1315
1379
  *
1316
- * Note: You cannot call `Component#setState` here.
1380
+ * Note: You cannot call {@link Component.setState} here.
1317
1381
  *
1318
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1319
- * prevents this from being invoked.
1382
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1383
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1384
+ * this from being invoked.
1320
1385
  *
1321
1386
  * @deprecated 16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17
1322
1387
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
@@ -1326,12 +1391,13 @@ declare namespace React {
1326
1391
  /**
1327
1392
  * Called immediately before rendering when new props or state is received. Not called for the initial render.
1328
1393
  *
1329
- * Note: You cannot call `Component#setState` here.
1394
+ * Note: You cannot call {@link Component.setState} here.
1330
1395
  *
1331
1396
  * This method will not stop working in React 17.
1332
1397
  *
1333
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1334
- * prevents this from being invoked.
1398
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1399
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1400
+ * this from being invoked.
1335
1401
  *
1336
1402
  * @deprecated 16.3, use getSnapshotBeforeUpdate instead
1337
1403
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
@@ -1341,7 +1407,9 @@ declare namespace React {
1341
1407
  }
1342
1408
 
1343
1409
  /**
1344
- * @deprecated https://legacy.reactjs.org/blog/2016/07/13/mixins-considered-harmful.html
1410
+ * @deprecated
1411
+ *
1412
+ * @see {@link https://legacy.reactjs.org/blog/2016/07/13/mixins-considered-harmful.html Mixins Considered Harmful}
1345
1413
  */
1346
1414
  interface Mixin<P, S> extends ComponentLifecycle<P, S> {
1347
1415
  mixins?: Array<Mixin<P, S>> | undefined;
@@ -1414,7 +1482,11 @@ declare namespace React {
1414
1482
  render: ForwardRefRenderFunction<T, P>,
1415
1483
  ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
1416
1484
 
1417
- /** Ensures that the props do not include ref at all */
1485
+ /**
1486
+ * Omits the 'ref' attribute from the given props object.
1487
+ *
1488
+ * @template P The props object type.
1489
+ */
1418
1490
  type PropsWithoutRef<P> =
1419
1491
  // Omit would not be sufficient for this. We'd like to avoid unnecessary mapping and need a distributive conditional to support unions.
1420
1492
  // see: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
react/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/react",
3
- "version": "18.2.53",
3
+ "version": "18.2.54",
4
4
  "description": "TypeScript definitions for react",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react",
6
6
  "license": "MIT",
@@ -201,6 +201,6 @@
201
201
  "@types/scheduler": "*",
202
202
  "csstype": "^3.0.2"
203
203
  },
204
- "typesPublisherContentHash": "792d7aa40aaa749ea92edb60082cf00457f171be9a223df0e937cb8fd288816b",
204
+ "typesPublisherContentHash": "922d20729fb53d28cfe96cc0465943dbba834f9fdb3686885737ee539e61333b",
205
205
  "typeScriptVersion": "4.6"
206
206
  }
react/ts5.0/index.d.ts CHANGED
@@ -101,6 +101,15 @@ declare namespace React {
101
101
  */
102
102
  type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
103
103
 
104
+ /**
105
+ * Represents any user-defined component, either as a function or a class.
106
+ *
107
+ * Similar to {@link ComponentType}, but without extra properties like
108
+ * {@link FunctionComponent.defaultProps defaultProps } and
109
+ * {@link ComponentClass.contextTypes contextTypes}.
110
+ *
111
+ * @template P The props the component accepts.
112
+ */
104
113
  type JSXElementConstructor<P> =
105
114
  | ((
106
115
  props: P,
@@ -137,6 +146,9 @@ declare namespace React {
137
146
  * ```
138
147
  */
139
148
  interface RefObject<T> {
149
+ /**
150
+ * The current value of the ref.
151
+ */
140
152
  readonly current: T | null;
141
153
  }
142
154
 
@@ -175,22 +187,23 @@ declare namespace React {
175
187
  * ```
176
188
  */
177
189
  type LegacyRef<T> = string | Ref<T>;
190
+
178
191
  /**
179
- * Gets the instance type for a React element. The instance will be different for various component types:
192
+ * Retrieves the type of the 'ref' prop for a given component type or tag name.
193
+ *
194
+ * @template C The component type.
180
195
  *
181
- * - React class components will be the class instance. So if you had `class Foo extends React.Component<{}> {}`
182
- * and used `React.ElementRef<typeof Foo>` then the type would be the instance of `Foo`.
183
- * - React stateless functional components do not have a backing instance and so `React.ElementRef<typeof Bar>`
184
- * (when `Bar` is `function Bar() {}`) will give you the `undefined` type.
185
- * - JSX intrinsics like `div` will give you their DOM instance. For `React.ElementRef<'div'>` that would be
186
- * `HTMLDivElement`. For `React.ElementRef<'input'>` that would be `HTMLInputElement`.
187
- * - React stateless functional components that forward a `ref` will give you the `ElementRef` of the forwarded
188
- * to component.
196
+ * @example
189
197
  *
190
- * `C` must be the type _of_ a React component so you need to use typeof as in `React.ElementRef<typeof MyComponent>`.
198
+ * ```tsx
199
+ * type MyComponentRef = React.ElementRef<typeof MyComponent>;
200
+ * ```
191
201
  *
192
- * @todo In Flow, this works a little different with forwarded refs and the `AbstractComponent` that
193
- * `React.forwardRef()` returns.
202
+ * @example
203
+ *
204
+ * ```tsx
205
+ * type DivRef = React.ElementRef<'div'>;
206
+ * ```
194
207
  */
195
208
  type ElementRef<
196
209
  C extends
@@ -231,6 +244,9 @@ declare namespace React {
231
244
  */
232
245
  ref?: Ref<T> | undefined;
233
246
  }
247
+ /**
248
+ * Represents the built-in attributes available to class components.
249
+ */
234
250
  interface ClassAttributes<T> extends Attributes {
235
251
  /**
236
252
  * Allows getting a ref to the component instance.
@@ -358,6 +374,34 @@ declare namespace React {
358
374
  * App or library types should never augment this interface.
359
375
  */
360
376
  interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES {}
377
+
378
+ /**
379
+ * Represents all of the things React can render.
380
+ *
381
+ * @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/reactnode/ React TypeScript Cheatsheet}
382
+ *
383
+ * @example
384
+ *
385
+ * ```tsx
386
+ * // Typing children
387
+ * type Props = { children: ReactNode }
388
+ *
389
+ * const Component = ({ children }: Props) => <div>{children}</div>
390
+ *
391
+ * <Component>hello</Component>
392
+ * ```
393
+ *
394
+ * @example
395
+ *
396
+ * ```tsx
397
+ * // Typing a custom element
398
+ * type Props = { customElement: ReactNode }
399
+ *
400
+ * const Component = ({ customElement }: Props) => <div>{customElement}</div>
401
+ *
402
+ * <Component customElement={<div>hello</div>} />
403
+ * ```
404
+ */
361
405
  type ReactNode =
362
406
  | ReactElement
363
407
  | string
@@ -477,12 +521,21 @@ declare namespace React {
477
521
  ...children: ReactNode[]
478
522
  ): ReactElement<P>;
479
523
 
480
- // Context via RenderProps
524
+ /**
525
+ * Describes the props accepted by a Context {@link Provider}.
526
+ *
527
+ * @template T The type of the value the context provides.
528
+ */
481
529
  interface ProviderProps<T> {
482
530
  value: T;
483
531
  children?: ReactNode | undefined;
484
532
  }
485
533
 
534
+ /**
535
+ * Describes the props accepted by a Context {@link Consumer}.
536
+ *
537
+ * @template T The type of the value the context provides.
538
+ */
486
539
  interface ConsumerProps<T> {
487
540
  children: (value: T) => ReactNode;
488
541
  }
@@ -639,7 +692,9 @@ declare namespace React {
639
692
 
640
693
  function isValidElement<P>(object: {} | null | undefined): object is ReactElement<P>;
641
694
 
642
- // Sync with `ReactChildren` until `ReactChildren` is removed.
695
+ /**
696
+ * Maintainer's note: Sync with {@link ReactChildren} until {@link ReactChildren} is removed.
697
+ */
643
698
  const Children: {
644
699
  map<T, C>(
645
700
  children: C | readonly C[],
@@ -822,7 +877,7 @@ declare namespace React {
822
877
  /**
823
878
  * If set, `this.context` will be set at runtime to the current value of the given Context.
824
879
  *
825
- * Usage:
880
+ * @example
826
881
  *
827
882
  * ```ts
828
883
  * type MyContext = number
@@ -846,6 +901,8 @@ declare namespace React {
846
901
  * `React.ContextType` of your `static contextType`.
847
902
  * Should be used with type annotation or static contextType.
848
903
  *
904
+ * @example
905
+ *
849
906
  * ```ts
850
907
  * static contextType = MyContext
851
908
  * // For TS pre-3.7:
@@ -1196,7 +1253,7 @@ declare namespace React {
1196
1253
  * `PureComponent` implements a shallow comparison on props and state and returns true if any
1197
1254
  * props or states have changed.
1198
1255
  *
1199
- * If false is returned, `Component#render`, `componentWillUpdate`
1256
+ * If false is returned, {@link Component.render}, `componentWillUpdate`
1200
1257
  * and `componentDidUpdate` will not be called.
1201
1258
  */
1202
1259
  shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
@@ -1238,29 +1295,30 @@ declare namespace React {
1238
1295
  // This should be "infer SS" but can't use it yet
1239
1296
  interface NewLifecycle<P, S, SS> {
1240
1297
  /**
1241
- * Runs before React applies the result of `render` to the document, and
1242
- * returns an object to be given to componentDidUpdate. Useful for saving
1243
- * things such as scroll position before `render` causes changes to it.
1298
+ * Runs before React applies the result of {@link Component.render render} to the document, and
1299
+ * returns an object to be given to {@link componentDidUpdate}. Useful for saving
1300
+ * things such as scroll position before {@link Component.render render} causes changes to it.
1244
1301
  *
1245
- * Note: the presence of getSnapshotBeforeUpdate prevents any of the deprecated
1302
+ * Note: the presence of this method prevents any of the deprecated
1246
1303
  * lifecycle events from running.
1247
1304
  */
1248
1305
  getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>): SS | null;
1249
1306
  /**
1250
1307
  * Called immediately after updating occurs. Not called for the initial render.
1251
1308
  *
1252
- * The snapshot is only present if getSnapshotBeforeUpdate is present and returns non-null.
1309
+ * The snapshot is only present if {@link getSnapshotBeforeUpdate} is present and returns non-null.
1253
1310
  */
1254
1311
  componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot?: SS): void;
1255
1312
  }
1256
1313
 
1257
1314
  interface DeprecatedLifecycle<P, S> {
1258
1315
  /**
1259
- * Called immediately before mounting occurs, and before `Component#render`.
1316
+ * Called immediately before mounting occurs, and before {@link Component.render}.
1260
1317
  * Avoid introducing any side-effects or subscriptions in this method.
1261
1318
  *
1262
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1263
- * prevents this from being invoked.
1319
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1320
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1321
+ * this from being invoked.
1264
1322
  *
1265
1323
  * @deprecated 16.3, use componentDidMount or the constructor instead; will stop working in React 17
1266
1324
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
@@ -1268,13 +1326,14 @@ declare namespace React {
1268
1326
  */
1269
1327
  componentWillMount?(): void;
1270
1328
  /**
1271
- * Called immediately before mounting occurs, and before `Component#render`.
1329
+ * Called immediately before mounting occurs, and before {@link Component.render}.
1272
1330
  * Avoid introducing any side-effects or subscriptions in this method.
1273
1331
  *
1274
1332
  * This method will not stop working in React 17.
1275
1333
  *
1276
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1277
- * prevents this from being invoked.
1334
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1335
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1336
+ * this from being invoked.
1278
1337
  *
1279
1338
  * @deprecated 16.3, use componentDidMount or the constructor instead
1280
1339
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
@@ -1286,10 +1345,11 @@ declare namespace React {
1286
1345
  * React may call this even if props have not changed, so be sure to compare new and existing
1287
1346
  * props if you only want to handle changes.
1288
1347
  *
1289
- * Calling `Component#setState` generally does not trigger this method.
1348
+ * Calling {@link Component.setState} generally does not trigger this method.
1290
1349
  *
1291
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1292
- * prevents this from being invoked.
1350
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1351
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1352
+ * this from being invoked.
1293
1353
  *
1294
1354
  * @deprecated 16.3, use static getDerivedStateFromProps instead; will stop working in React 17
1295
1355
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
@@ -1301,12 +1361,13 @@ declare namespace React {
1301
1361
  * React may call this even if props have not changed, so be sure to compare new and existing
1302
1362
  * props if you only want to handle changes.
1303
1363
  *
1304
- * Calling `Component#setState` generally does not trigger this method.
1364
+ * Calling {@link Component.setState} generally does not trigger this method.
1305
1365
  *
1306
1366
  * This method will not stop working in React 17.
1307
1367
  *
1308
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1309
- * prevents this from being invoked.
1368
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1369
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1370
+ * this from being invoked.
1310
1371
  *
1311
1372
  * @deprecated 16.3, use static getDerivedStateFromProps instead
1312
1373
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
@@ -1316,10 +1377,11 @@ declare namespace React {
1316
1377
  /**
1317
1378
  * Called immediately before rendering when new props or state is received. Not called for the initial render.
1318
1379
  *
1319
- * Note: You cannot call `Component#setState` here.
1380
+ * Note: You cannot call {@link Component.setState} here.
1320
1381
  *
1321
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1322
- * prevents this from being invoked.
1382
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1383
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1384
+ * this from being invoked.
1323
1385
  *
1324
1386
  * @deprecated 16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17
1325
1387
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
@@ -1329,12 +1391,13 @@ declare namespace React {
1329
1391
  /**
1330
1392
  * Called immediately before rendering when new props or state is received. Not called for the initial render.
1331
1393
  *
1332
- * Note: You cannot call `Component#setState` here.
1394
+ * Note: You cannot call {@link Component.setState} here.
1333
1395
  *
1334
1396
  * This method will not stop working in React 17.
1335
1397
  *
1336
- * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps
1337
- * prevents this from being invoked.
1398
+ * Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
1399
+ * or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
1400
+ * this from being invoked.
1338
1401
  *
1339
1402
  * @deprecated 16.3, use getSnapshotBeforeUpdate instead
1340
1403
  * @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
@@ -1417,7 +1480,11 @@ declare namespace React {
1417
1480
  render: ForwardRefRenderFunction<T, P>,
1418
1481
  ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
1419
1482
 
1420
- /** Ensures that the props do not include ref at all */
1483
+ /**
1484
+ * Omits the 'ref' attribute from the given props object.
1485
+ *
1486
+ * @template P The props object type.
1487
+ */
1421
1488
  type PropsWithoutRef<P> =
1422
1489
  // Omit would not be sufficient for this. We'd like to avoid unnecessary mapping and need a distributive conditional to support unions.
1423
1490
  // see: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types