Skip to content

Commit

Permalink
[react] Add types for experimental_use
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Sep 10, 2022
1 parent 55c08b6 commit fb0058a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions types/react/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,31 @@ declare module '.' {
* @see https://reactjs.org/docs/concurrent-mode-patterns.html#suspenselist
*/
export const SuspenseList: ExoticComponent<SuspenseListProps>;

interface ThenableImpl<T> {
then(onFulfill: (value: T) => unknown, onReject: (error: unknown) => unknown): void | PromiseLike<unknown>;
}
interface UntrackedThenable<T> extends ThenableImpl<T> {
status?: void;
}

export interface PendingThenable<T> extends ThenableImpl<T> {
status: 'pending';
}

export interface FulfilledThenable<T> extends ThenableImpl<T> {
status: 'fulfilled';
value: T;
}

export interface RejectedThenable<T> extends ThenableImpl<T> {
status: 'rejected';
reason: unknown;
}

export type Thenable<T> = UntrackedThenable<T> | PendingThenable<T> | FulfilledThenable<T> | RejectedThenable<T>;

export type Usable<T> = Thenable<T> | Context<T>;

export function experimental_use<T>(usable: Usable<T>): T;
}
16 changes: 16 additions & 0 deletions types/react/test/experimental.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ function suspenseTest() {
<React.Suspense fallback="Loading">A</React.Suspense>
<React.Suspense fallback="Loading">B</React.Suspense>
</React.SuspenseList>;

const contextUsers = React.createContext(['HAL']);
const promisedUsers = Promise.resolve(['Dave']);

function useUse() {
// @ts-expect-error Missing value
React.experimental_use();

// $ExpectType string[]
const users = React.experimental_use(promisedUsers);
// @ts-expect-error incompatible type. Mainly to potentially inspect TypeScript error message
React.experimental_use({});

// $ExpectType string[]
const contextValue = React.experimental_use(contextUsers);
}

0 comments on commit fb0058a

Please sign in to comment.