UNPKG

5.98 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
33type NativeToggleEvent = ToggleEvent;
34
35declare module "." {
36 export type Usable<T> = PromiseLike<T> | Context<T>;
37
38 export function use<T>(usable: Usable<T>): T;
39
40 interface ServerContextJSONArray extends ReadonlyArray<ServerContextJSONValue> {}
41 export type ServerContextJSONValue =
42 | string
43 | boolean
44 | number
45 | null
46 | ServerContextJSONArray
47 | { [key: string]: ServerContextJSONValue };
48 export interface ServerContext<T extends ServerContextJSONValue> {
49 Provider: Provider<T>;
50 }
51 /**
52 * Accepts a context object (the value returned from `React.createContext` or `React.createServerContext`) and returns the current
53 * context value, as given by the nearest context provider for the given context.
54 *
55 * @version 16.8.0
56 * @see https://react.dev/reference/react/useContext
57 */
58 function useContext<T extends ServerContextJSONValue>(context: ServerContext<T>): T;
59 export function createServerContext<T extends ServerContextJSONValue>(
60 globalName: string,
61 defaultValue: T,
62 ): ServerContext<T>;
63
64 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
65 export function cache<CachedFunction extends Function>(fn: CachedFunction): CachedFunction;
66
67 export function unstable_useCacheRefresh(): () => void;
68
69 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_FORM_ACTIONS {
70 functions: (formData: FormData) => void | Promise<void>;
71 }
72
73 export interface TransitionStartFunction {
74 /**
75 * Marks all state updates inside the async function as transitions
76 *
77 * @see {https://react.dev/reference/react/useTransition#starttransition}
78 *
79 * @param callback
80 */
81 (callback: () => Promise<VoidOrUndefinedOnly>): void;
82 }
83
84 /**
85 * Similar to `useTransition` but allows uses where hooks are not available.
86 *
87 * @param callback An _asynchronous_ function which causes state updates that can be deferred.
88 */
89 export function startTransition(scope: () => Promise<VoidOrUndefinedOnly>): void;
90
91 export function useOptimistic<State>(
92 passthrough: State,
93 ): [State, (action: State | ((pendingState: State) => State)) => void];
94 export function useOptimistic<State, Action>(
95 passthrough: State,
96 reducer: (state: State, action: Action) => State,
97 ): [State, (action: Action) => void];
98
99 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES {
100 cleanup: () => VoidOrUndefinedOnly;
101 }
102
103 export function useActionState<State>(
104 action: (state: Awaited<State>) => State | Promise<State>,
105 initialState: Awaited<State>,
106 permalink?: string,
107 ): [state: Awaited<State>, dispatch: () => void, isPending: boolean];
108 export function useActionState<State, Payload>(
109 action: (state: Awaited<State>, payload: Payload) => State | Promise<State>,
110 initialState: Awaited<State>,
111 permalink?: string,
112 ): [state: Awaited<State>, dispatch: (payload: Payload) => void, isPending: boolean];
113
114 interface DOMAttributes<T> {
115 // Transition Events
116 onTransitionCancel?: TransitionEventHandler<T> | undefined;
117 onTransitionCancelCapture?: TransitionEventHandler<T> | undefined;
118 onTransitionRun?: TransitionEventHandler<T> | undefined;
119 onTransitionRunCapture?: TransitionEventHandler<T> | undefined;
120 onTransitionStart?: TransitionEventHandler<T> | undefined;
121 onTransitionStartCapture?: TransitionEventHandler<T> | undefined;
122 }
123
124 type ToggleEventHandler<T = Element> = EventHandler<ToggleEvent<T>>;
125
126 interface HTMLAttributes<T> {
127 popover?: "" | "auto" | "manual" | undefined;
128 popoverTargetAction?: "toggle" | "show" | "hide" | undefined;
129 popoverTarget?: string | undefined;
130 onToggle?: ToggleEventHandler<T> | undefined;
131 onBeforeToggle?: ToggleEventHandler<T> | undefined;
132 }
133
134 interface ToggleEvent<T = Element> extends SyntheticEvent<T, NativeToggleEvent> {
135 oldState: "closed" | "open";
136 newState: "closed" | "open";
137 }
138
139 interface LinkHTMLAttributes<T> {
140 precedence?: string | undefined;
141 }
142
143 interface StyleHTMLAttributes<T> {
144 href?: string | undefined;
145 precedence?: string | undefined;
146 }
147
148 /**
149 * @internal Use `Awaited<ReactNode>` instead
150 */
151 // Helper type to enable `Awaited<ReactNode>`.
152 // Must be a copy of the non-thenables of `ReactNode`.
153 type AwaitedReactNode =
154 | ReactElement
155 | string
156 | number
157 | Iterable<AwaitedReactNode>
158 | ReactPortal
159 | boolean
160 | null
161 | undefined;
162 interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES {
163 promises: Promise<AwaitedReactNode>;
164 bigints: bigint;
165 }
166}