UNPKG

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