UNPKG

3.85 kBTypeScriptView Raw
1export type ReactComponentLike =
2 | string
3 | ((props: any, context?: any) => any)
4 | (new(props: any, context?: any) => any);
5
6export interface ReactElementLike {
7 type: ReactComponentLike;
8 props: any;
9 key: string | null;
10}
11
12export interface ReactNodeArray extends Iterable<ReactNodeLike> {}
13
14export type ReactNodeLike =
15 | ReactElementLike
16 | ReactNodeArray
17 | string
18 | number
19 | boolean
20 | null
21 | undefined;
22
23export const nominalTypeHack: unique symbol;
24
25export type IsOptional<T> = undefined extends T ? true : false;
26
27export type RequiredKeys<V> = {
28 [K in keyof V]-?: Exclude<V[K], undefined> extends Validator<infer T> ? IsOptional<T> extends true ? never : K
29 : never;
30}[keyof V];
31export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
32export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]> };
33
34export interface Validator<T> {
35 (
36 props: { [key: string]: any },
37 propName: string,
38 componentName: string,
39 location: string,
40 propFullName: string,
41 ): Error | null;
42 [nominalTypeHack]?: {
43 type: T;
44 } | undefined;
45}
46
47export interface Requireable<T> extends Validator<T | undefined | null> {
48 isRequired: Validator<NonNullable<T>>;
49}
50
51export type ValidationMap<T> = { [K in keyof T]?: Validator<T[K]> };
52
53/**
54 * Like {@link ValidationMap} but treats `undefined`, `null` and optional properties the same.
55 * This type is only added as a migration path in React 19 where this type was removed from React.
56 * Runtime and compile time types would mismatch since you could see `undefined` at runtime when your types don't expect this type.
57 */
58export type WeakValidationMap<T> = {
59 [K in keyof T]?: null extends T[K] ? Validator<T[K] | null | undefined>
60 : undefined extends T[K] ? Validator<T[K] | null | undefined>
61 : Validator<T[K]>;
62};
63
64export type InferType<V> = V extends Validator<infer T> ? T : any;
65export type InferProps<V> =
66 & InferPropsInner<Pick<V, RequiredKeys<V>>>
67 & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;
68
69export const any: Requireable<any>;
70export const array: Requireable<any[]>;
71export const bool: Requireable<boolean>;
72export const func: Requireable<(...args: any[]) => any>;
73export const number: Requireable<number>;
74export const object: Requireable<object>;
75export const string: Requireable<string>;
76export const node: Requireable<ReactNodeLike>;
77export const element: Requireable<ReactElementLike>;
78export const symbol: Requireable<symbol>;
79export const elementType: Requireable<ReactComponentLike>;
80export function instanceOf<T>(expectedClass: new(...args: any[]) => T): Requireable<T>;
81export function oneOf<T>(types: readonly T[]): Requireable<T>;
82export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>;
83export function arrayOf<T>(type: Validator<T>): Requireable<T[]>;
84export function objectOf<T>(type: Validator<T>): Requireable<{ [K in keyof any]: T }>;
85export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>;
86export function exact<P extends ValidationMap<any>>(type: P): Requireable<Required<InferProps<P>>>;
87
88/**
89 * Assert that the values match with the type specs.
90 * Error messages are memorized and will only be shown once.
91 *
92 * @param typeSpecs Map of name to a ReactPropType
93 * @param values Runtime values that need to be type-checked
94 * @param location e.g. "prop", "context", "child context"
95 * @param componentName Name of the component for error messages
96 * @param getStack Returns the component stack
97 */
98export function checkPropTypes(
99 typeSpecs: any,
100 values: any,
101 location: string,
102 componentName: string,
103 getStack?: () => any,
104): void;
105
106/**
107 * Only available if NODE_ENV=production
108 */
109export function resetWarningCache(): void;