UNPKG

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