|
| 1 | +import type * as PropTypes from "prop-types"; |
| 2 | + |
| 3 | +interface Component<P = {}, S = {}, SS = any> |
| 4 | + extends ComponentLifecycle<P, S, SS> {} |
| 5 | +class Component<P, S> { |
| 6 | + // tslint won't let me format the sample code in a way that vscode likes it :( |
| 7 | + /** |
| 8 | + * If set, `this.context` will be set at runtime to the current value of the given Context. |
| 9 | + * |
| 10 | + * Usage: |
| 11 | + * |
| 12 | + * ```ts |
| 13 | + * type MyContext = number |
| 14 | + * const Ctx = React.createContext<MyContext>(0) |
| 15 | + * |
| 16 | + * class Foo extends React.Component { |
| 17 | + * static contextType = Ctx |
| 18 | + * context!: React.ContextType<typeof Ctx> |
| 19 | + * render () { |
| 20 | + * return <>My context's value: {this.context}</>; |
| 21 | + * } |
| 22 | + * } |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * @see https://reactjs.org/docs/context.html#classcontexttype |
| 26 | + */ |
| 27 | + static contextType?: Context<any> | undefined; |
| 28 | + |
| 29 | + /** |
| 30 | + * If using the new style context, re-declare this in your class to be the |
| 31 | + * `React.ContextType` of your `static contextType`. |
| 32 | + * Should be used with type annotation or static contextType. |
| 33 | + * |
| 34 | + * ```ts |
| 35 | + * static contextType = MyContext |
| 36 | + * // For TS pre-3.7: |
| 37 | + * context!: React.ContextType<typeof MyContext> |
| 38 | + * // For TS 3.7 and above: |
| 39 | + * declare context: React.ContextType<typeof MyContext> |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @see https://reactjs.org/docs/context.html |
| 43 | + */ |
| 44 | + context: unknown; |
| 45 | + |
| 46 | + constructor(props: Readonly<P> | P); |
| 47 | + /** |
| 48 | + * @deprecated |
| 49 | + * @see https://reactjs.org/docs/legacy-context.html |
| 50 | + */ |
| 51 | + constructor(props: P, context: any); |
| 52 | + |
| 53 | + // We MUST keep setState() as a unified signature because it allows proper checking of the method return type. |
| 54 | + // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257 |
| 55 | + // Also, the ` | S` allows intellisense to not be dumbisense |
| 56 | + setState<K extends keyof S>( |
| 57 | + state: |
| 58 | + | ((prevState: Readonly<S>, props: Readonly<P>) => Pick<S, K> | S | null) |
| 59 | + | (Pick<S, K> | S | null), |
| 60 | + callback?: () => void |
| 61 | + ): void; |
| 62 | + |
| 63 | + forceUpdate(callback?: () => void): void; |
| 64 | + render(): ReactNode; |
| 65 | + |
| 66 | + readonly props: Readonly<P>; |
| 67 | + state: Readonly<S>; |
| 68 | + /** |
| 69 | + * @deprecated |
| 70 | + * https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs |
| 71 | + */ |
| 72 | + refs: { |
| 73 | + [key: string]: ReactInstance; |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +interface ComponentClass<P = {}, S = ComponentState> |
| 78 | + extends StaticLifecycle<P, S> { |
| 79 | + new (props: P, context?: any): Component<P, S>; |
| 80 | + propTypes?: WeakValidationMap<P> | undefined; |
| 81 | + contextType?: Context<any> | undefined; |
| 82 | + contextTypes?: ValidationMap<any> | undefined; |
| 83 | + childContextTypes?: ValidationMap<any> | undefined; |
| 84 | + defaultProps?: Partial<P> | undefined; |
| 85 | + displayName?: string | undefined; |
| 86 | +} |
| 87 | + |
| 88 | +interface ComponentLifecycle<P, S, SS = any> |
| 89 | + extends NewLifecycle<P, S, SS>, |
| 90 | + DeprecatedLifecycle<P, S> { |
| 91 | + /** |
| 92 | + * Called immediately after a component is mounted. Setting state here will trigger re-rendering. |
| 93 | + */ |
| 94 | + componentDidMount?(): void; |
| 95 | + /** |
| 96 | + * Called to determine whether the change in props and state should trigger a re-render. |
| 97 | + * |
| 98 | + * `Component` always returns true. |
| 99 | + * `PureComponent` implements a shallow comparison on props and state and returns true if any |
| 100 | + * props or states have changed. |
| 101 | + * |
| 102 | + * If false is returned, `Component#render`, `componentWillUpdate` |
| 103 | + * and `componentDidUpdate` will not be called. |
| 104 | + */ |
| 105 | + shouldComponentUpdate?( |
| 106 | + nextProps: Readonly<P>, |
| 107 | + nextState: Readonly<S>, |
| 108 | + nextContext: any |
| 109 | + ): boolean; |
| 110 | + /** |
| 111 | + * Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as |
| 112 | + * cancelled network requests, or cleaning up any DOM elements created in `componentDidMount`. |
| 113 | + */ |
| 114 | + componentWillUnmount?(): void; |
| 115 | + /** |
| 116 | + * Catches exceptions generated in descendant components. Unhandled exceptions will cause |
| 117 | + * the entire component tree to unmount. |
| 118 | + */ |
| 119 | + componentDidCatch?(error: Error, errorInfo: ErrorInfo): void; |
| 120 | +} |
| 121 | + |
| 122 | +type ComponentState = any; |
| 123 | + |
| 124 | +export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>; |
| 125 | + |
| 126 | +type Consumer<T> = ExoticComponent<ConsumerProps<T>>; |
| 127 | + |
| 128 | +interface ConsumerProps<T> { |
| 129 | + children: (value: T) => ReactNode; |
| 130 | +} |
| 131 | + |
| 132 | +interface Context<T> { |
| 133 | + Provider: Provider<T>; |
| 134 | + Consumer: Consumer<T>; |
| 135 | + displayName?: string | undefined; |
| 136 | +} |
| 137 | + |
| 138 | +interface DeprecatedLifecycle<P, S> { |
| 139 | + /** |
| 140 | + * Called immediately before mounting occurs, and before `Component#render`. |
| 141 | + * Avoid introducing any side-effects or subscriptions in this method. |
| 142 | + * |
| 143 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 144 | + * prevents this from being invoked. |
| 145 | + * |
| 146 | + * @deprecated 16.3, use componentDidMount or the constructor instead; will stop working in React 17 |
| 147 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state |
| 148 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 149 | + */ |
| 150 | + componentWillMount?(): void; |
| 151 | + /** |
| 152 | + * Called immediately before mounting occurs, and before `Component#render`. |
| 153 | + * Avoid introducing any side-effects or subscriptions in this method. |
| 154 | + * |
| 155 | + * This method will not stop working in React 17. |
| 156 | + * |
| 157 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 158 | + * prevents this from being invoked. |
| 159 | + * |
| 160 | + * @deprecated 16.3, use componentDidMount or the constructor instead |
| 161 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state |
| 162 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 163 | + */ |
| 164 | + UNSAFE_componentWillMount?(): void; |
| 165 | + /** |
| 166 | + * Called when the component may be receiving new props. |
| 167 | + * React may call this even if props have not changed, so be sure to compare new and existing |
| 168 | + * props if you only want to handle changes. |
| 169 | + * |
| 170 | + * Calling `Component#setState` generally does not trigger this method. |
| 171 | + * |
| 172 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 173 | + * prevents this from being invoked. |
| 174 | + * |
| 175 | + * @deprecated 16.3, use static getDerivedStateFromProps instead; will stop working in React 17 |
| 176 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props |
| 177 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 178 | + */ |
| 179 | + componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void; |
| 180 | + /** |
| 181 | + * Called when the component may be receiving new props. |
| 182 | + * React may call this even if props have not changed, so be sure to compare new and existing |
| 183 | + * props if you only want to handle changes. |
| 184 | + * |
| 185 | + * Calling `Component#setState` generally does not trigger this method. |
| 186 | + * |
| 187 | + * This method will not stop working in React 17. |
| 188 | + * |
| 189 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 190 | + * prevents this from being invoked. |
| 191 | + * |
| 192 | + * @deprecated 16.3, use static getDerivedStateFromProps instead |
| 193 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props |
| 194 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 195 | + */ |
| 196 | + UNSAFE_componentWillReceiveProps?( |
| 197 | + nextProps: Readonly<P>, |
| 198 | + nextContext: any |
| 199 | + ): void; |
| 200 | + /** |
| 201 | + * Called immediately before rendering when new props or state is received. Not called for the initial render. |
| 202 | + * |
| 203 | + * Note: You cannot call `Component#setState` here. |
| 204 | + * |
| 205 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 206 | + * prevents this from being invoked. |
| 207 | + * |
| 208 | + * @deprecated 16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17 |
| 209 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update |
| 210 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 211 | + */ |
| 212 | + componentWillUpdate?( |
| 213 | + nextProps: Readonly<P>, |
| 214 | + nextState: Readonly<S>, |
| 215 | + nextContext: any |
| 216 | + ): void; |
| 217 | + /** |
| 218 | + * Called immediately before rendering when new props or state is received. Not called for the initial render. |
| 219 | + * |
| 220 | + * Note: You cannot call `Component#setState` here. |
| 221 | + * |
| 222 | + * This method will not stop working in React 17. |
| 223 | + * |
| 224 | + * Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps |
| 225 | + * prevents this from being invoked. |
| 226 | + * |
| 227 | + * @deprecated 16.3, use getSnapshotBeforeUpdate instead |
| 228 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update |
| 229 | + * @see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path |
| 230 | + */ |
| 231 | + UNSAFE_componentWillUpdate?( |
| 232 | + nextProps: Readonly<P>, |
| 233 | + nextState: Readonly<S>, |
| 234 | + nextContext: any |
| 235 | + ): void; |
| 236 | +} |
| 237 | + |
| 238 | +interface ErrorInfo { |
| 239 | + /** |
| 240 | + * Captures which component contained the exception, and its ancestors. |
| 241 | + */ |
| 242 | + componentStack: string; |
| 243 | +} |
| 244 | + |
| 245 | +interface ExoticComponent<P = {}> { |
| 246 | + /** |
| 247 | + * **NOTE**: Exotic components are not callable. |
| 248 | + */ |
| 249 | + (props: P): ReactElement | null; |
| 250 | + readonly $$typeof: symbol; |
| 251 | +} |
| 252 | + |
| 253 | +interface FunctionComponent<P = {}> { |
| 254 | + (props: P, context?: any): ReactElement<any, any> | null; |
| 255 | + propTypes?: WeakValidationMap<P> | undefined; |
| 256 | + contextTypes?: ValidationMap<any> | undefined; |
| 257 | + defaultProps?: Partial<P> | undefined; |
| 258 | + displayName?: string | undefined; |
| 259 | +} |
| 260 | + |
| 261 | +type GetDerivedStateFromProps<P, S> = |
| 262 | + /** |
| 263 | + * Returns an update to a component's state based on its new props and old state. |
| 264 | + * |
| 265 | + * Note: its presence prevents any of the deprecated lifecycle methods from being invoked |
| 266 | + */ |
| 267 | + (nextProps: Readonly<P>, prevState: S) => Partial<S> | null; |
| 268 | + |
| 269 | +type GetDerivedStateFromError<P, S> = |
| 270 | + /** |
| 271 | + * This lifecycle is invoked after an error has been thrown by a descendant component. |
| 272 | + * It receives the error that was thrown as a parameter and should return a value to update state. |
| 273 | + * |
| 274 | + * Note: its presence prevents any of the deprecated lifecycle methods from being invoked |
| 275 | + */ |
| 276 | + (error: any) => Partial<S> | null; |
| 277 | + |
| 278 | +type JSXElementConstructor<P> = |
| 279 | + | ((props: P) => ReactElement<any, any> | null) |
| 280 | + | (new (props: P) => Component<any, any>); |
| 281 | + |
| 282 | +type Key = string | number; |
| 283 | + |
| 284 | +interface NewLifecycle<P, S, SS> { |
| 285 | + /** |
| 286 | + * Runs before React applies the result of `render` to the document, and |
| 287 | + * returns an object to be given to componentDidUpdate. Useful for saving |
| 288 | + * things such as scroll position before `render` causes changes to it. |
| 289 | + * |
| 290 | + * Note: the presence of getSnapshotBeforeUpdate prevents any of the deprecated |
| 291 | + * lifecycle events from running. |
| 292 | + */ |
| 293 | + getSnapshotBeforeUpdate?( |
| 294 | + prevProps: Readonly<P>, |
| 295 | + prevState: Readonly<S> |
| 296 | + ): SS | null; |
| 297 | + /** |
| 298 | + * Called immediately after updating occurs. Not called for the initial render. |
| 299 | + * |
| 300 | + * The snapshot is only present if getSnapshotBeforeUpdate is present and returns non-null. |
| 301 | + */ |
| 302 | + componentDidUpdate?( |
| 303 | + prevProps: Readonly<P>, |
| 304 | + prevState: Readonly<S>, |
| 305 | + snapshot?: SS |
| 306 | + ): void; |
| 307 | +} |
| 308 | + |
| 309 | +type Provider<T> = ProviderExoticComponent<ProviderProps<T>>; |
| 310 | + |
| 311 | +interface ProviderExoticComponent<P> extends ExoticComponent<P> { |
| 312 | + propTypes?: WeakValidationMap<P> | undefined; |
| 313 | +} |
| 314 | + |
| 315 | +interface ProviderProps<T> { |
| 316 | + value: T; |
| 317 | + children?: ReactNode | undefined; |
| 318 | +} |
| 319 | + |
| 320 | +interface ReactElement< |
| 321 | + P = any, |
| 322 | + T extends string | JSXElementConstructor<any> = |
| 323 | + | string |
| 324 | + | JSXElementConstructor<any> |
| 325 | +> { |
| 326 | + type: T; |
| 327 | + props: P; |
| 328 | + key: Key | null; |
| 329 | +} |
| 330 | + |
| 331 | +type ReactFragment = Iterable<ReactNode>; |
| 332 | + |
| 333 | +type ReactInstance = Component<any> | Element; |
| 334 | + |
| 335 | +type ReactNode = |
| 336 | + | ReactElement |
| 337 | + | string |
| 338 | + | number |
| 339 | + | ReactFragment |
| 340 | + | ReactPortal |
| 341 | + | boolean |
| 342 | + | null |
| 343 | + | undefined; |
| 344 | + |
| 345 | +interface ReactPortal extends ReactElement { |
| 346 | + key: Key | null; |
| 347 | + children: ReactNode; |
| 348 | +} |
| 349 | + |
| 350 | +interface StaticLifecycle<P, S> { |
| 351 | + getDerivedStateFromProps?: GetDerivedStateFromProps<P, S> | undefined; |
| 352 | + getDerivedStateFromError?: GetDerivedStateFromError<P, S> | undefined; |
| 353 | +} |
| 354 | + |
| 355 | +type ValidationMap<T> = PropTypes.ValidationMap<T>; |
| 356 | + |
| 357 | +type Validator<T> = PropTypes.Validator<T>; |
| 358 | + |
| 359 | +type WeakValidationMap<T> = { |
| 360 | + [K in keyof T]?: null extends T[K] |
| 361 | + ? Validator<T[K] | null | undefined> |
| 362 | + : undefined extends T[K] |
| 363 | + ? Validator<T[K] | null | undefined> |
| 364 | + : Validator<T[K]>; |
| 365 | +}; |
0 commit comments