summaryrefslogtreecommitdiff
path: root/std/types/react/prop-types.d.ts
blob: 0bb0e47e908e479f80d476205f1cdb9968aaeec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// These types are adapted from
// https://github.com/DefinitelyTyped/DefinitelyTyped to work under Deno.
//
// Type definitions for prop-types 15.7
// Project: https://github.com/reactjs/prop-types, https://facebook.github.io/react
// Definitions by: DovydasNavickas <https://github.com/DovydasNavickas>
//                 Ferdy Budhidharma <https://github.com/ferdaber>
//                 Sebastian Silbermann <https://github.com/eps1lon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8

/* eslint-disable */
export type ReactComponentLike =
  | string
  | ((props: any, context?: any) => any)
  | (new (props: any, context?: any) => any);

export interface ReactElementLike {
  type: ReactComponentLike;
  props: any;
  key: string | number | null;
}

export interface ReactNodeArray extends Array<ReactNodeLike> {}

export type ReactNodeLike =
  | {}
  | ReactElementLike
  | ReactNodeArray
  | string
  | number
  | boolean
  | null
  | undefined;

export const nominalTypeHack: unique symbol;

export type IsOptional<T> = undefined extends T ? true : false;

export type RequiredKeys<V> = {
  [K in keyof V]-?: Exclude<V[K], undefined> extends Validator<infer T>
    ? IsOptional<T> extends true
      ? never
      : K
    : never;
}[keyof V];
export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]> };

export interface Validator<T> {
  (
    props: { [key: string]: any },
    propName: string,
    componentName: string,
    location: string,
    propFullName: string
  ): Error | null;
  [nominalTypeHack]?: {
    type: T;
  };
}

export interface Requireable<T> extends Validator<T | undefined | null> {
  isRequired: Validator<NonNullable<T>>;
}

export type ValidationMap<T> = { [K in keyof T]?: Validator<T[K]> };

export type InferType<V> = V extends Validator<infer T> ? T : any;
export type InferProps<V> = InferPropsInner<Pick<V, RequiredKeys<V>>> &
  Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;

export const any: Requireable<any>;
export const array: Requireable<any[]>;
export const bool: Requireable<boolean>;
export const func: Requireable<(...args: any[]) => any>;
export const number: Requireable<number>;
export const object: Requireable<object>;
export const string: Requireable<string>;
export const node: Requireable<ReactNodeLike>;
export const element: Requireable<ReactElementLike>;
export const symbol: Requireable<symbol>;
export const elementType: Requireable<ReactComponentLike>;
export function instanceOf<T>(
  expectedClass: new (...args: any[]) => T
): Requireable<T>;
export function oneOf<T>(types: ReadonlyArray<T>): Requireable<T>;
export function oneOfType<T extends Validator<any>>(
  types: T[]
): Requireable<NonNullable<InferType<T>>>;
export function arrayOf<T>(type: Validator<T>): Requireable<T[]>;
export function objectOf<T>(
  type: Validator<T>
): Requireable<{ [K in keyof any]: T }>;
export function shape<P extends ValidationMap<any>>(
  type: P
): Requireable<InferProps<P>>;
export function exact<P extends ValidationMap<any>>(
  type: P
): Requireable<Required<InferProps<P>>>;

/**
 * Assert that the values match with the type specs.
 * Error messages are memorized and will only be shown once.
 *
 * @param typeSpecs Map of name to a ReactPropType
 * @param values Runtime values that need to be type-checked
 * @param location e.g. "prop", "context", "child context"
 * @param componentName Name of the component for error messages
 * @param getStack Returns the component stack
 */
export function checkPropTypes(
  typeSpecs: any,
  values: any,
  location: string,
  componentName: string,
  getStack?: () => any
): void;

/**
 * Only available if NODE_ENV=production
 */
export function resetWarningCache(): void;