File: propTypeValidatorInference.types

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (299 lines) | stat: -rw-r--r-- 17,576 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
=== tests/cases/compiler/node_modules/prop-types/index.d.ts ===
export const nominalTypeHack: unique symbol;
>nominalTypeHack : unique symbol

export type IsOptional<T> = undefined | null extends T ? true : undefined extends T ? true : null extends T ? true : false;
>IsOptional : IsOptional<T>
>null : null
>true : true
>true : true
>null : null
>true : true
>false : 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];
>RequiredKeys : RequiredKeys<V>
>true : true

export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>;
>OptionalKeys : OptionalKeys<V>

export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]>; };
>InferPropsInner : InferPropsInner<V>

export interface Validator<T> {
    (props: object, propName: string, componentName: string, location: string, propFullName: string): Error | null;
>props : object
>propName : string
>componentName : string
>location : string
>propFullName : string
>null : null

    [nominalTypeHack]?: T;
>[nominalTypeHack] : T | undefined
>nominalTypeHack : unique symbol
}

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

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

export type InferType<V> = V extends Validator<infer T> ? T : any;
>InferType : InferType<V>

export type InferProps<V> =
>InferProps : InferProps<V>

    & InferPropsInner<Pick<V, RequiredKeys<V>>>
    & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>;

export const any: Requireable<any>;
>any : Requireable<any>

export const array: Requireable<any[]>;
>array : Requireable<any[]>

export const bool: Requireable<boolean>;
>bool : Requireable<boolean>

export const string: Requireable<string>;
>string : Requireable<string>

export const number: Requireable<number>;
>number : Requireable<number>

export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>;
>shape : <P extends ValidationMap<any>>(type: P) => Requireable<InferProps<P>>
>type : P

export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>;
>oneOfType : <T extends Validator<any>>(types: T[]) => Requireable<NonNullable<InferType<T>>>
>types : T[]


=== tests/cases/compiler/file.ts ===
import * as PropTypes from "prop-types";
>PropTypes : typeof PropTypes

interface Props {
    any?: any;
>any : any

    array: string[];
>array : string[]

    bool: boolean;
>bool : boolean

    shape: {
>shape : { foo: string; bar?: boolean | undefined; baz?: any; }

        foo: string;
>foo : string

        bar?: boolean;
>bar : boolean | undefined

        baz?: any
>baz : any

    };
    oneOfType: string | boolean | {
>oneOfType : string | boolean | { foo?: string | undefined; bar: number; }

        foo?: string;
>foo : string | undefined

        bar: number;
>bar : number

    };
}

type PropTypesMap = PropTypes.ValidationMap<Props>;
>PropTypesMap : PropTypes.ValidationMap<Props>
>PropTypes : any

const innerProps = {
>innerProps : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }
>{    foo: PropTypes.string.isRequired,    bar: PropTypes.bool,    baz: PropTypes.any} : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }

    foo: PropTypes.string.isRequired,
>foo : PropTypes.Validator<string>
>PropTypes.string.isRequired : PropTypes.Validator<string>
>PropTypes.string : PropTypes.Requireable<string>
>PropTypes : typeof PropTypes
>string : PropTypes.Requireable<string>
>isRequired : PropTypes.Validator<string>

    bar: PropTypes.bool,
>bar : PropTypes.Requireable<boolean>
>PropTypes.bool : PropTypes.Requireable<boolean>
>PropTypes : typeof PropTypes
>bool : PropTypes.Requireable<boolean>

    baz: PropTypes.any
>baz : PropTypes.Requireable<any>
>PropTypes.any : PropTypes.Requireable<any>
>PropTypes : typeof PropTypes
>any : PropTypes.Requireable<any>

};

const arrayOfTypes = [PropTypes.string, PropTypes.bool, PropTypes.shape({
>arrayOfTypes : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>[PropTypes.string, PropTypes.bool, PropTypes.shape({    foo: PropTypes.string,    bar: PropTypes.number.isRequired})] : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>PropTypes.string : PropTypes.Requireable<string>
>PropTypes : typeof PropTypes
>string : PropTypes.Requireable<string>
>PropTypes.bool : PropTypes.Requireable<boolean>
>PropTypes : typeof PropTypes
>bool : PropTypes.Requireable<boolean>
>PropTypes.shape({    foo: PropTypes.string,    bar: PropTypes.number.isRequired}) : PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>
>PropTypes.shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>PropTypes : typeof PropTypes
>shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>{    foo: PropTypes.string,    bar: PropTypes.number.isRequired} : { foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }

    foo: PropTypes.string,
>foo : PropTypes.Requireable<string>
>PropTypes.string : PropTypes.Requireable<string>
>PropTypes : typeof PropTypes
>string : PropTypes.Requireable<string>

    bar: PropTypes.number.isRequired
>bar : PropTypes.Validator<number>
>PropTypes.number.isRequired : PropTypes.Validator<number>
>PropTypes.number : PropTypes.Requireable<number>
>PropTypes : typeof PropTypes
>number : PropTypes.Requireable<number>
>isRequired : PropTypes.Validator<number>

})];

// TS checking
const propTypes: PropTypesMap = {
>propTypes : PropTypes.ValidationMap<Props>
>{    any: PropTypes.any,    array: PropTypes.array.isRequired,    bool: PropTypes.bool.isRequired,    shape: PropTypes.shape(innerProps).isRequired,    oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }

    any: PropTypes.any,
>any : PropTypes.Requireable<any>
>PropTypes.any : PropTypes.Requireable<any>
>PropTypes : typeof PropTypes
>any : PropTypes.Requireable<any>

    array: PropTypes.array.isRequired,
>array : PropTypes.Validator<any[]>
>PropTypes.array.isRequired : PropTypes.Validator<any[]>
>PropTypes.array : PropTypes.Requireable<any[]>
>PropTypes : typeof PropTypes
>array : PropTypes.Requireable<any[]>
>isRequired : PropTypes.Validator<any[]>

    bool: PropTypes.bool.isRequired,
>bool : PropTypes.Validator<boolean>
>PropTypes.bool.isRequired : PropTypes.Validator<boolean>
>PropTypes.bool : PropTypes.Requireable<boolean>
>PropTypes : typeof PropTypes
>bool : PropTypes.Requireable<boolean>
>isRequired : PropTypes.Validator<boolean>

    shape: PropTypes.shape(innerProps).isRequired,
>shape : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps) : PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>PropTypes : typeof PropTypes
>shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>innerProps : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }
>isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>

    oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,
>oneOfType : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>
>PropTypes.oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>PropTypes : typeof PropTypes
>oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>arrayOfTypes : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>

};

// JS checking
const propTypesWithoutAnnotation = {
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }
>{    any: PropTypes.any,    array: PropTypes.array.isRequired,    bool: PropTypes.bool.isRequired,    shape: PropTypes.shape(innerProps).isRequired,    oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,} : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }

    any: PropTypes.any,
>any : PropTypes.Requireable<any>
>PropTypes.any : PropTypes.Requireable<any>
>PropTypes : typeof PropTypes
>any : PropTypes.Requireable<any>

    array: PropTypes.array.isRequired,
>array : PropTypes.Validator<any[]>
>PropTypes.array.isRequired : PropTypes.Validator<any[]>
>PropTypes.array : PropTypes.Requireable<any[]>
>PropTypes : typeof PropTypes
>array : PropTypes.Requireable<any[]>
>isRequired : PropTypes.Validator<any[]>

    bool: PropTypes.bool.isRequired,
>bool : PropTypes.Validator<boolean>
>PropTypes.bool.isRequired : PropTypes.Validator<boolean>
>PropTypes.bool : PropTypes.Requireable<boolean>
>PropTypes : typeof PropTypes
>bool : PropTypes.Requireable<boolean>
>isRequired : PropTypes.Validator<boolean>

    shape: PropTypes.shape(innerProps).isRequired,
>shape : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps).isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>
>PropTypes.shape(innerProps) : PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>
>PropTypes.shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>PropTypes : typeof PropTypes
>shape : <P extends PropTypes.ValidationMap<any>>(type: P) => PropTypes.Requireable<PropTypes.InferProps<P>>
>innerProps : { foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }
>isRequired : PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>

    oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired,
>oneOfType : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes).isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>
>PropTypes.oneOfType(arrayOfTypes) : PropTypes.Requireable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>
>PropTypes.oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>PropTypes : typeof PropTypes
>oneOfType : <T extends PropTypes.Validator<any>>(types: T[]) => PropTypes.Requireable<NonNullable<PropTypes.InferType<T>>>
>arrayOfTypes : (PropTypes.Requireable<boolean> | PropTypes.Requireable<string> | PropTypes.Requireable<PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>)[]
>isRequired : PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>

};

type ExtractedProps = PropTypes.InferProps<typeof propTypes>;
>ExtractedProps : PropTypes.InferPropsInner<Pick<PropTypes.ValidationMap<Props>, PropTypes.RequiredKeys<PropTypes.ValidationMap<Props>>>> & Partial<PropTypes.InferPropsInner<Pick<PropTypes.ValidationMap<Props>, "any">>>
>PropTypes : any
>propTypes : PropTypes.ValidationMap<Props>

type ExtractedPropsWithoutAnnotation = PropTypes.InferProps<typeof propTypesWithoutAnnotation>;
>ExtractedPropsWithoutAnnotation : PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }, PropTypes.RequiredKeys<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }>>> & Partial<PropTypes.InferPropsInner<Pick<{ any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }, "any">>>
>PropTypes : any
>propTypesWithoutAnnotation : { any: PropTypes.Requireable<any>; array: PropTypes.Validator<any[]>; bool: PropTypes.Validator<boolean>; shape: PropTypes.Validator<NonNullable<PropTypes.InferProps<{ foo: PropTypes.Validator<string>; bar: PropTypes.Requireable<boolean>; baz: PropTypes.Requireable<any>; }>>>; oneOfType: PropTypes.Validator<NonNullable<NonNullable<string | boolean | PropTypes.InferProps<{ foo: PropTypes.Requireable<string>; bar: PropTypes.Validator<number>; }>>>>; }

type ExtractPropsMatch = ExtractedProps extends ExtractedPropsWithoutAnnotation ? true : false;
>ExtractPropsMatch : true
>true : true
>false : false

const x: true = (null as any as ExtractPropsMatch);
>x : true
>true : true
>(null as any as ExtractPropsMatch) : true
>null as any as ExtractPropsMatch : true
>null as any : any
>null : null