UNPKG

3.66 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 Iterable<ReactNodeLike> {}
21
22export type ReactNodeLike =
23 | ReactElementLike
24 | ReactNodeArray
25 | string
26 | number
27 | boolean
28 | null
29 | undefined;
30
31export const nominalTypeHack: unique symbol;
32
33export type IsOptional<T> = undefined extends T ? true : false;
34
35export type RequiredKeys<V> = { [K in keyof V]-?: Exclude<V[K], undefined> extends Validator<infer T> ? IsOptional<T> extends true ? never : K : never }[keyof V];
36export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
37export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]>; };
38
39export interface Validator<T> {
40 (props: { [key: string]: any }, propName: string, componentName: string, location: string, propFullName: string): Error | null;
41 [nominalTypeHack]?: {
42 type: T;
43 } | undefined;
44}
45
46export interface Requireable<T> extends Validator<T | undefined | null> {
47 isRequired: Validator<NonNullable<T>>;
48}
49
50export type ValidationMap<T> = { [K in keyof T]?: Validator<T[K]> };
51
52export type InferType<V> = V extends Validator<infer T> ? T : any;
53export type InferProps<V> =
54 & InferPropsInner<Pick<V, RequiredKeys<V>>>
55 & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;
56
57export const any: Requireable<any>;
58export const array: Requireable<any[]>;
59export const bool: Requireable<boolean>;
60export const func: Requireable<(...args: any[]) => any>;
61export const number: Requireable<number>;
62export const object: Requireable<object>;
63export const string: Requireable<string>;
64export const node: Requireable<ReactNodeLike>;
65export const element: Requireable<ReactElementLike>;
66export const symbol: Requireable<symbol>;
67export const elementType: Requireable<ReactComponentLike>;
68export function instanceOf<T>(expectedClass: new (...args: any[]) => T): Requireable<T>;
69export function oneOf<T>(types: ReadonlyArray<T>): Requireable<T>;
70export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>;
71export function arrayOf<T>(type: Validator<T>): Requireable<T[]>;
72export function objectOf<T>(type: Validator<T>): Requireable<{ [K in keyof any]: T; }>;
73export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>;
74export function exact<P extends ValidationMap<any>>(type: P): Requireable<Required<InferProps<P>>>;
75
76/**
77 * Assert that the values match with the type specs.
78 * Error messages are memorized and will only be shown once.
79 *
80 * @param typeSpecs Map of name to a ReactPropType
81 * @param values Runtime values that need to be type-checked
82 * @param location e.g. "prop", "context", "child context"
83 * @param componentName Name of the component for error messages
84 * @param getStack Returns the component stack
85 */
86export function checkPropTypes(typeSpecs: any, values: any, location: string, componentName: string, getStack?: () => any): void;
87
88/**
89 * Only available if NODE_ENV=production
90 */
91export function resetWarningCache(): void;