|
| 1 | +tests/cases/compiler/file.ts(35,5): error TS2322: Type 'Validator<InferProps<{ foo: Validator<string>; bar: Requireable<boolean>; baz: Requireable<any>; }>>' is not assignable to type 'Validator<{ foo: string; bar?: boolean | undefined; baz?: any; }>'. |
| 2 | + Type 'InferProps<{ foo: Validator<string>; bar: Requireable<boolean>; baz: Requireable<any>; }>' is not assignable to type '{ foo: string; bar?: boolean | undefined; baz?: any; }'. |
| 3 | + Types of property 'bar' are incompatible. |
| 4 | + Type 'boolean | null | undefined' is not assignable to type 'boolean | undefined'. |
| 5 | + Type 'null' is not assignable to type 'boolean | undefined'. |
| 6 | +tests/cases/compiler/file.ts(36,5): error TS2322: Type 'Validator<string | boolean | InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>>' is not assignable to type 'Validator<string | boolean | { foo?: string | undefined; bar: number; }>'. |
| 7 | + Type 'string | boolean | InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type 'string | boolean | { foo?: string | undefined; bar: number; }'. |
| 8 | + Type 'InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type 'string | boolean | { foo?: string | undefined; bar: number; }'. |
| 9 | + Type 'InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type '{ foo?: string | undefined; bar: number; }'. |
| 10 | + Types of property 'foo' are incompatible. |
| 11 | + Type 'string | null | undefined' is not assignable to type 'string | undefined'. |
| 12 | + Type 'null' is not assignable to type 'string | undefined'. |
| 13 | + |
| 14 | + |
| 15 | +==== tests/cases/compiler/node_modules/prop-types/index.d.ts (0 errors) ==== |
| 16 | + export const nominalTypeHack: unique symbol; |
| 17 | + |
| 18 | + export type IsOptional<T> = undefined | null extends T ? true : undefined extends T ? true : null extends T ? true : false; |
| 19 | + |
| 20 | + 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]; |
| 21 | + export type OptionalKeys<V> = Exclude<keyof V, RequiredKeys<V>>; |
| 22 | + export type InferPropsInner<V> = { [K in keyof V]-?: InferType<V[K]>; }; |
| 23 | + |
| 24 | + export interface Validator<T> { |
| 25 | + (props: object, propName: string, componentName: string, location: string, propFullName: string): Error | null; |
| 26 | + [nominalTypeHack]?: T; |
| 27 | + } |
| 28 | + |
| 29 | + export interface Requireable<T> extends Validator<T | undefined | null> { |
| 30 | + isRequired: Validator<NonNullable<T>>; |
| 31 | + } |
| 32 | + |
| 33 | + export type ValidationMap<T> = { [K in keyof T]?: Validator<T[K]> }; |
| 34 | + |
| 35 | + export type InferType<V> = V extends Validator<infer T> ? T : any; |
| 36 | + export type InferProps<V> = |
| 37 | + & InferPropsInner<Pick<V, RequiredKeys<V>>> |
| 38 | + & Partial<InferPropsInner<Pick<V, OptionalKeys<V>>>>; |
| 39 | + |
| 40 | + export const any: Requireable<any>; |
| 41 | + export const array: Requireable<any[]>; |
| 42 | + export const bool: Requireable<boolean>; |
| 43 | + export const string: Requireable<string>; |
| 44 | + export const number: Requireable<number>; |
| 45 | + export function shape<P extends ValidationMap<any>>(type: P): Requireable<InferProps<P>>; |
| 46 | + export function oneOfType<T extends Validator<any>>(types: T[]): Requireable<NonNullable<InferType<T>>>; |
| 47 | + |
| 48 | + |
| 49 | +==== tests/cases/compiler/file.ts (2 errors) ==== |
| 50 | + import * as PropTypes from "prop-types"; |
| 51 | + interface Props { |
| 52 | + any?: any; |
| 53 | + array: string[]; |
| 54 | + bool: boolean; |
| 55 | + shape: { |
| 56 | + foo: string; |
| 57 | + bar?: boolean; |
| 58 | + baz?: any |
| 59 | + }; |
| 60 | + oneOfType: string | boolean | { |
| 61 | + foo?: string; |
| 62 | + bar: number; |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + type PropTypesMap = PropTypes.ValidationMap<Props>; |
| 67 | + |
| 68 | + const innerProps = { |
| 69 | + foo: PropTypes.string.isRequired, |
| 70 | + bar: PropTypes.bool, |
| 71 | + baz: PropTypes.any |
| 72 | + }; |
| 73 | + |
| 74 | + const arrayOfTypes = [PropTypes.string, PropTypes.bool, PropTypes.shape({ |
| 75 | + foo: PropTypes.string, |
| 76 | + bar: PropTypes.number.isRequired |
| 77 | + })]; |
| 78 | + |
| 79 | + // TS checking |
| 80 | + const propTypes: PropTypesMap = { |
| 81 | + any: PropTypes.any, |
| 82 | + array: PropTypes.array.isRequired, |
| 83 | + bool: PropTypes.bool.isRequired, |
| 84 | + shape: PropTypes.shape(innerProps).isRequired, |
| 85 | + ~~~~~ |
| 86 | +!!! error TS2322: Type 'Validator<InferProps<{ foo: Validator<string>; bar: Requireable<boolean>; baz: Requireable<any>; }>>' is not assignable to type 'Validator<{ foo: string; bar?: boolean | undefined; baz?: any; }>'. |
| 87 | +!!! error TS2322: Type 'InferProps<{ foo: Validator<string>; bar: Requireable<boolean>; baz: Requireable<any>; }>' is not assignable to type '{ foo: string; bar?: boolean | undefined; baz?: any; }'. |
| 88 | +!!! error TS2322: Types of property 'bar' are incompatible. |
| 89 | +!!! error TS2322: Type 'boolean | null | undefined' is not assignable to type 'boolean | undefined'. |
| 90 | +!!! error TS2322: Type 'null' is not assignable to type 'boolean | undefined'. |
| 91 | +!!! related TS6500 tests/cases/compiler/file.ts:6:5: The expected type comes from property 'shape' which is declared here on type 'ValidationMap<Props>' |
| 92 | + oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired, |
| 93 | + ~~~~~~~~~ |
| 94 | +!!! error TS2322: Type 'Validator<string | boolean | InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>>' is not assignable to type 'Validator<string | boolean | { foo?: string | undefined; bar: number; }>'. |
| 95 | +!!! error TS2322: Type 'string | boolean | InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type 'string | boolean | { foo?: string | undefined; bar: number; }'. |
| 96 | +!!! error TS2322: Type 'InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type 'string | boolean | { foo?: string | undefined; bar: number; }'. |
| 97 | +!!! error TS2322: Type 'InferProps<{ foo: Requireable<string>; bar: Validator<number>; }>' is not assignable to type '{ foo?: string | undefined; bar: number; }'. |
| 98 | +!!! error TS2322: Types of property 'foo' are incompatible. |
| 99 | +!!! error TS2322: Type 'string | null | undefined' is not assignable to type 'string | undefined'. |
| 100 | +!!! error TS2322: Type 'null' is not assignable to type 'string | undefined'. |
| 101 | +!!! related TS6500 tests/cases/compiler/file.ts:11:5: The expected type comes from property 'oneOfType' which is declared here on type 'ValidationMap<Props>' |
| 102 | + }; |
| 103 | + |
| 104 | + // JS checking |
| 105 | + const propTypesWithoutAnnotation = { |
| 106 | + any: PropTypes.any, |
| 107 | + array: PropTypes.array.isRequired, |
| 108 | + bool: PropTypes.bool.isRequired, |
| 109 | + shape: PropTypes.shape(innerProps).isRequired, |
| 110 | + oneOfType: PropTypes.oneOfType(arrayOfTypes).isRequired, |
| 111 | + }; |
| 112 | + |
| 113 | + type ExtractedProps = PropTypes.InferProps<typeof propTypes>; |
| 114 | + |
| 115 | + type ExtractedPropsWithoutAnnotation = PropTypes.InferProps<typeof propTypesWithoutAnnotation>; |
| 116 | + |
| 117 | + type ExtractPropsMatch = ExtractedProps extends ExtractedPropsWithoutAnnotation ? true : false; |
| 118 | + const x: true = (null as any as ExtractPropsMatch); |
0 commit comments