UNPKG

4.55 kBTypeScriptView Raw
1/**
2 * These are types for things that are present in the React `canary` release channel.
3 *
4 * To load the types declared here in an actual project, there are three ways. The easiest one,
5 * if your `tsconfig.json` already has a `"types"` array in the `"compilerOptions"` section,
6 * is to add `"react/canary"` to the `"types"` array.
7 *
8 * Alternatively, a specific import syntax can to be used from a typescript file.
9 * This module does not exist in reality, which is why the {} is important:
10 *
11 * ```ts
12 * import {} from 'react/canary'
13 * ```
14 *
15 * It is also possible to include it through a triple-slash reference:
16 *
17 * ```ts
18 * /// <reference types="react/canary" />
19 * ```
20 *
21 * Either the import or the reference only needs to appear once, anywhere in the project.
22 */
23
24// See https://github.com/facebook/react/blob/main/packages/react/src/React.js to see how the exports are declared,
25
26import React = require(".");
27
28export {};
29
30declare const UNDEFINED_VOID_ONLY: unique symbol;
31type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
32
33declare module "." {
34 interface ThenableImpl<T> {
35 then(onFulfill: (value: T) => unknown, onReject: (error: unknown) => unknown): void | PromiseLike<unknown>;
36 }
37 interface UntrackedThenable<T> extends ThenableImpl<T> {
38 status?: void;
39 }
40
41 export interface PendingThenable<T> extends ThenableImpl<T> {
42 status: "pending";
43 }
44
45 export interface FulfilledThenable<T> extends ThenableImpl<T> {
46 status: "fulfilled";
47 value: T;
48 }
49
50 export interface RejectedThenable<T> extends ThenableImpl<T> {
51 status: "rejected";
52 reason: unknown;
53 }
54
55 export type Thenable<T> = UntrackedThenable<T> | PendingThenable<T> | FulfilledThenable<T> | RejectedThenable<T>;
56
57 export type Usable<T> = Thenable<T> | Context<T>;
58
59 export function use<T>(usable: Usable<T>): T;
60
61 interface ServerContextJSONArray extends ReadonlyArray<ServerContextJSONValue> {}
62 export type ServerContextJSONValue =
63 | string
64 | boolean
65 | number
66 | null
67 | ServerContextJSONArray
68 | { [key: string]: ServerContextJSONValue };
69 export interface ServerContext<T extends ServerContextJSONValue> {
70 Provider: Provider<T>;
71 }
72 /**
73 * Accepts a context object (the value returned from `React.createContext` or `React.createServerContext`) and returns the current
74 * context value, as given by the nearest context provider for the given context.
75 *
76 * @version 16.8.0
77 * @see https://react.dev/reference/react/useContext
78 */
79 function useContext<T extends ServerContextJSONValue>(context: ServerContext<T>): T;
80 export function createServerContext<T extends ServerContextJSONValue>(
81 globalName: string,
82 defaultValue: T,
83 ): ServerContext<T>;
84
85 // eslint-disable-next-line @typescript-eslint/ban-types
86 export function cache<CachedFunction extends Function>(fn: CachedFunction): CachedFunction;
87
88 export function unstable_useCacheRefresh(): () => void;
89
90 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_FORM_ACTIONS {
91 functions: (formData: FormData) => void;
92 }
93
94 export interface TransitionStartFunction {
95 /**
96 * Marks all state updates inside the async function as transitions
97 *
98 * @see {https://react.dev/reference/react/useTransition#starttransition}
99 *
100 * @param callback
101 */
102 (callback: () => Promise<VoidOrUndefinedOnly>): void;
103 }
104
105 export function useOptimistic<State>(
106 passthrough: State,
107 ): [State, (action: State | ((pendingState: State) => State)) => void];
108 export function useOptimistic<State, Action>(
109 passthrough: State,
110 reducer: (state: State, action: Action) => State,
111 ): [State, (action: Action) => void];
112
113 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES {
114 cleanup: () => VoidOrUndefinedOnly;
115 }
116
117 export function useActionState<State>(
118 action: (state: Awaited<State>) => State | Promise<State>,
119 initialState: Awaited<State>,
120 permalink?: string,
121 ): [state: Awaited<State>, dispatch: () => void, isPending: boolean];
122 export function useActionState<State, Payload>(
123 action: (state: Awaited<State>, payload: Payload) => State | Promise<State>,
124 initialState: Awaited<State>,
125 permalink?: string,
126 ): [state: Awaited<State>, dispatch: (payload: Payload) => void, isPending: boolean];
127}