UNPKG

3.67 kBTypeScriptView Raw
1// Type definitions for prop-types 15.7
2// Project: https://github.com/reactjs/prop-types, https://facebook.github.io/react
3// Definitions by: DovydasNavickas <https://github.com/DovydasNavickas>
4// Ferdy Budhidharma <https://github.com/ferdaber>
5// Sebastian Silbermann <https://github.com/eps1lon>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.8
8
9export type ReactComponentLike =
10 | string
11 | ((props: any, context?: any) => any)
12 | (new (props: any, context?: any) => any);
13
14export interface ReactElementLike {
15 type: ReactComponentLike;
16 props: any;
17 key: string | number | null;
18}
19
20export interface ReactNodeArray extends Array<ReactNodeLike> {}
21
22export type ReactNodeLike =
23 | {}
24 | ReactElementLike
25 | ReactNodeArray
26 | string
27 | number
28 | boolean
29 | null
30 | undefined;
31
32export const nominalTypeHack: unique symbol;
33
34export type IsOptional<T> = undefined extends T ? true : false;
35
36export type RequiredKeys<V> = { [K in keyof V]-?: Exclude<V[K], undefined> extends Validator<infer T> ? IsOptional<T> extends true ? never : K : never }[keyof V];
37export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
38export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]>; };
39
40export interface Validator<T> {
41 (props: { [key: string]: any }, propName: string, componentName: string, location: string, propFullName: string): 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
53export type InferType<V> = V extends Validator<infer T> ? T : any;
54export type InferProps<V> =
55 & InferPropsInner<Pick<V, RequiredKeys<V>>>
56 & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;
57
58export const any: Requireable<any>;
59export const array: Requireable<any[]>;
60export const bool: Requireable<boolean>;
61export const func: Requireable<(...args: any[]) => any>;
62export const number: Requireable<number>;
63export const object: Requireable<object>;
64export const string: Requireable<string>;
65export const node: Requireable<ReactNodeLike>;
66export const element: Requireable<ReactElementLike>;
67export const symbol: Requireable<symbol>;
68export const elementType: Requireable<ReactComponentLike>;
69export function instanceOf<T>(expectedClass: new (...args: any[]) => T): Requireable<T>;
70export function oneOf<T>(types: ReadonlyArray<T>): Requireable<T>;
71export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>;
72export function arrayOf<T>(type: Validator<T>): Requireable<T[]>;
73export function objectOf<T>(type: Validator<T>): Requireable<{ [K in keyof any]: T; }>;
74export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>;
75export function exact<P extends ValidationMap<any>>(type: P): Requireable<Required<InferProps<P>>>;
76
77/**
78 * Assert that the values match with the type specs.
79 * Error messages are memorized and will only be shown once.
80 *
81 * @param typeSpecs Map of name to a ReactPropType
82 * @param values Runtime values that need to be type-checked
83 * @param location e.g. "prop", "context", "child context"
84 * @param componentName Name of the component for error messages
85 * @param getStack Returns the component stack
86 */
87export function checkPropTypes(typeSpecs: any, values: any, location: string, componentName: string, getStack?: () => any): void;
88
89/**
90 * Only available if NODE_ENV=production
91 */
92export function resetWarningCache(): void;