Skip to content

Commit 0cc4d1e

Browse files
authored
[react] Add types for optimisticKey (DefinitelyTyped#74453)
1 parent 72123f4 commit 0cc4d1e

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

types/react/experimental.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ declare module "." {
174174
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_MEDIA_SRC_TYPES {
175175
srcObject: Blob | MediaSource | MediaStream;
176176
}
177+
178+
// @enableOptimisticKey
179+
export const optimisticKey: unique symbol;
180+
181+
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES {
182+
optimisticKey: typeof optimisticKey;
183+
}
177184
}

types/react/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,20 @@ declare namespace React {
226226

227227
type ComponentState = any;
228228

229+
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES {}
230+
229231
/**
230232
* A value which uniquely identifies a node among items in an array.
231233
*
232234
* @see {@link https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key React Docs}
233235
*/
234-
type Key = string | number | bigint;
236+
type Key =
237+
| string
238+
| number
239+
| bigint
240+
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES[
241+
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES
242+
];
235243

236244
/**
237245
* @internal The props any component can receive.

types/react/test/experimental.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,11 @@ function swipeTransitionTest() {
209209
>
210210
</React.ViewTransition>;
211211
}
212+
213+
function optimisticKeyTest() {
214+
<div key={React.optimisticKey} />;
215+
<div
216+
// @ts-expect-error -- random symbols are not allowed.
217+
key={Symbol("foreign-key")}
218+
/>;
219+
}

types/react/ts5.0/experimental.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ declare module "." {
174174
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_MEDIA_SRC_TYPES {
175175
srcObject: Blob | MediaSource | MediaStream;
176176
}
177+
178+
// @enableOptimisticKey
179+
export const optimisticKey: unique symbol;
180+
181+
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES {
182+
optimisticKey: typeof optimisticKey;
183+
}
177184
}

types/react/ts5.0/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,20 @@ declare namespace React {
226226

227227
type ComponentState = any;
228228

229+
interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES {}
230+
229231
/**
230232
* A value which uniquely identifies a node among items in an array.
231233
*
232234
* @see {@link https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key React Docs}
233235
*/
234-
type Key = string | number | bigint;
236+
type Key =
237+
| string
238+
| number
239+
| bigint
240+
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES[
241+
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_KEY_TYPES
242+
];
235243

236244
/**
237245
* @internal The props any component can receive.

types/react/ts5.0/test/experimental.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,11 @@ function swipeTransitionTest() {
209209
>
210210
</React.ViewTransition>;
211211
}
212+
213+
function optimisticKeyTest() {
214+
<div key={React.optimisticKey} />;
215+
<div
216+
// @ts-expect-error -- random symbols are not allowed.
217+
key={Symbol("foreign-key")}
218+
/>;
219+
}

0 commit comments

Comments
 (0)