1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { ReactNode, ReactElement } from 'react';
|
9 |
|
10 | export const nominalTypeHack: unique symbol;
|
11 |
|
12 | export type IsOptional<T> = undefined | null extends T ? true : undefined extends T ? true : null extends T ? true : false;
|
13 |
|
14 | export type RequiredKeys<V> = { [K in keyof V]: V[K] extends Validator<infer T> ? IsOptional<T> extends true ? never : K : never }[keyof V];
|
15 | export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
|
16 | export type InferPropsInner<V> = { [K in keyof V]: InferType<V[K]>; };
|
17 |
|
18 | export interface Validator<T> {
|
19 | (props: object, propName: string, componentName: string, location: string, propFullName: string): Error | null;
|
20 | [nominalTypeHack]?: T;
|
21 | }
|
22 |
|
23 | export interface Requireable<T> extends Validator<T | undefined | null> {
|
24 | isRequired: Validator<NonNullable<T>>;
|
25 | }
|
26 |
|
27 | export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
|
28 |
|
29 | export type InferType<V> = V extends Validator<infer T> ? T : any;
|
30 | export type InferProps<V> =
|
31 | & InferPropsInner<Pick<V, RequiredKeys<V>>>
|
32 | & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;
|
33 |
|
34 | export const any: Requireable<any>;
|
35 | export const array: Requireable<any[]>;
|
36 | export const bool: Requireable<boolean>;
|
37 | export const func: Requireable<(...args: any[]) => any>;
|
38 | export const number: Requireable<number>;
|
39 | export const object: Requireable<object>;
|
40 | export const string: Requireable<string>;
|
41 | export const node: Requireable<ReactNode>;
|
42 | export const element: Requireable<ReactElement<any>>;
|
43 | export const symbol: Requireable<symbol>;
|
44 | export function instanceOf<T>(expectedClass: new (...args: any[]) => T): Requireable<T>;
|
45 | export function oneOf<T>(types: T[]): Requireable<T>;
|
46 | export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>;
|
47 | export function arrayOf<T>(type: Validator<T>): Requireable<T[]>;
|
48 | export function objectOf<T>(type: Validator<T>): Requireable<{ [K in keyof any]: T; }>;
|
49 | export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export function checkPropTypes(typeSpecs: any, values: any, location: string, componentName: string, getStack?: () => any): void;
|