|
| 1 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(55,12): error TS2322: Type '{ foo: number; }' is not assignable to type 'Defaultize<InferredPropTypes<{ foo: PropTypeChecker<number, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<string, true>; }>, { foo: number; }>'. |
| 2 | + Type '{ foo: number; }' is not assignable to type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: string; }'. |
| 3 | + Property 'bar' is missing in type '{ foo: number; }'. |
| 4 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(57,41): error TS2339: Property 'bat' does not exist on type 'Defaultize<InferredPropTypes<{ foo: PropTypeChecker<number, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<string, true>; }>, { foo: number; }>'. |
| 5 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(59,42): error TS2326: Types of property 'baz' are incompatible. |
| 6 | + Type 'null' is not assignable to type 'string'. |
| 7 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(69,26): error TS2326: Types of property 'foo' are incompatible. |
| 8 | + Type 'string' is not assignable to type 'number | null | undefined'. |
| 9 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(71,35): error TS2326: Types of property 'bar' are incompatible. |
| 10 | + Type 'null' is not assignable to type 'ReactNode'. |
| 11 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(80,38): error TS2339: Property 'bar' does not exist on type 'Defaultize<{}, { foo: number; }>'. |
| 12 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(81,29): error TS2326: Types of property 'foo' are incompatible. |
| 13 | + Type 'string' is not assignable to type 'number | undefined'. |
| 14 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(98,12): error TS2322: Type '{ foo: string; }' is not assignable to type 'Defaultize<FooProps & InferredPropTypes<{ foo: PropTypeChecker<string, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<number, true>; }>, { foo: string; }>'. |
| 15 | + Type '{ foo: string; }' is not assignable to type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: number; }'. |
| 16 | + Property 'bar' is missing in type '{ foo: string; }'. |
| 17 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(100,56): error TS2339: Property 'bat' does not exist on type 'Defaultize<FooProps & InferredPropTypes<{ foo: PropTypeChecker<string, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<number, true>; }>, { foo: string; }>'. |
| 18 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(102,57): error TS2326: Types of property 'baz' are incompatible. |
| 19 | + Type 'null' is not assignable to type 'number'. |
| 20 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(111,46): error TS2326: Types of property 'foo' are incompatible. |
| 21 | + Type 'number' is not assignable to type 'string'. |
| 22 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(112,46): error TS2326: Types of property 'foo' are incompatible. |
| 23 | + Type 'null' is not assignable to type 'string'. |
| 24 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(113,57): error TS2326: Types of property 'bar' are incompatible. |
| 25 | + Type 'null' is not assignable to type 'ReactNode'. |
| 26 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(122,58): error TS2339: Property 'bar' does not exist on type 'Defaultize<FooProps, { foo: string; }>'. |
| 27 | +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS2326: Types of property 'foo' are incompatible. |
| 28 | + Type 'number' is not assignable to type 'string | undefined'. |
| 29 | + |
| 30 | + |
| 31 | +==== tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx (15 errors) ==== |
| 32 | + type Defaultize<TProps, TDefaults> = |
| 33 | + & {[K in Extract<keyof TProps, keyof TDefaults>]?: TProps[K]} |
| 34 | + & {[K in Exclude<keyof TProps, keyof TDefaults>]: TProps[K]} |
| 35 | + & Partial<TDefaults>; |
| 36 | + |
| 37 | + type InferredPropTypes<P> = {[K in keyof P]: P[K] extends PropTypeChecker<infer T, infer U> ? PropTypeChecker<T, U>[typeof checkedType] : {}}; |
| 38 | + |
| 39 | + declare const checkedType: unique symbol; |
| 40 | + interface PropTypeChecker<U, TRequired = false> { |
| 41 | + (props: any, propName: string, componentName: string, location: any, propFullName: string): boolean; |
| 42 | + isRequired: PropTypeChecker<U, true>; |
| 43 | + [checkedType]: TRequired extends true ? U : U | null | undefined; |
| 44 | + } |
| 45 | + |
| 46 | + declare namespace PropTypes { |
| 47 | + export const number: PropTypeChecker<number>; |
| 48 | + export const string: PropTypeChecker<string>; |
| 49 | + export const node: PropTypeChecker<ReactNode>; |
| 50 | + } |
| 51 | + |
| 52 | + type ReactNode = string | number | ReactComponent<{}, {}>; |
| 53 | + |
| 54 | + declare class ReactComponent<P={}, S={}> { |
| 55 | + constructor(props: P); |
| 56 | + props: P & Readonly<{children: ReactNode[]}>; |
| 57 | + setState(s: Partial<S>): S; |
| 58 | + render(): ReactNode; |
| 59 | + } |
| 60 | + |
| 61 | + declare namespace JSX { |
| 62 | + interface Element extends ReactComponent {} |
| 63 | + interface IntrinsicElements {} |
| 64 | + type LibraryManagedAttributes<TComponent, TProps> = |
| 65 | + TComponent extends { defaultProps: infer D; propTypes: infer P; } |
| 66 | + ? Defaultize<TProps & InferredPropTypes<P>, D> |
| 67 | + : TComponent extends { defaultProps: infer D } |
| 68 | + ? Defaultize<TProps, D> |
| 69 | + : TComponent extends { propTypes: infer P } |
| 70 | + ? TProps & InferredPropTypes<P> |
| 71 | + : TProps; |
| 72 | + } |
| 73 | + |
| 74 | + class Component extends ReactComponent { |
| 75 | + static propTypes = { |
| 76 | + foo: PropTypes.number, |
| 77 | + bar: PropTypes.node, |
| 78 | + baz: PropTypes.string.isRequired, |
| 79 | + }; |
| 80 | + static defaultProps = { |
| 81 | + foo: 42, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const a = <Component foo={12} bar="yes" baz="yeah" />; |
| 86 | + const b = <Component foo={12} />; // Error, missing required prop bar |
| 87 | + ~~~~~~~~~ |
| 88 | +!!! error TS2322: Type '{ foo: number; }' is not assignable to type 'Defaultize<InferredPropTypes<{ foo: PropTypeChecker<number, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<string, true>; }>, { foo: number; }>'. |
| 89 | +!!! error TS2322: Type '{ foo: number; }' is not assignable to type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: string; }'. |
| 90 | +!!! error TS2322: Property 'bar' is missing in type '{ foo: number; }'. |
| 91 | + const c = <Component bar="yes" baz="yeah" />; |
| 92 | + const d = <Component bar="yes" baz="yo" bat="ohno" />; // Error, baz not a valid prop |
| 93 | + ~~~~~~~~~~ |
| 94 | +!!! error TS2339: Property 'bat' does not exist on type 'Defaultize<InferredPropTypes<{ foo: PropTypeChecker<number, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<string, true>; }>, { foo: number; }>'. |
| 95 | + const e = <Component foo={12} bar={null} baz="cool" />; // bar is nullable/undefinable since it's not marked `isRequired` |
| 96 | + const f = <Component foo={12} bar="yeah" baz={null} />; // Error, baz is _not_ nullable/undefinable since it's marked `isRequired` |
| 97 | + ~~~~~~~~~~ |
| 98 | +!!! error TS2326: Types of property 'baz' are incompatible. |
| 99 | +!!! error TS2326: Type 'null' is not assignable to type 'string'. |
| 100 | + |
| 101 | + class JustPropTypes extends ReactComponent { |
| 102 | + static propTypes = { |
| 103 | + foo: PropTypes.number, |
| 104 | + bar: PropTypes.node.isRequired, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + const g = <JustPropTypes foo={12} bar="ok" />; |
| 109 | + const h = <JustPropTypes foo="no" />; // error, wrong type |
| 110 | + ~~~~~~~~ |
| 111 | +!!! error TS2326: Types of property 'foo' are incompatible. |
| 112 | +!!! error TS2326: Type 'string' is not assignable to type 'number | null | undefined'. |
| 113 | + const i = <JustPropTypes foo={null} bar="ok" />; |
| 114 | + const j = <JustPropTypes foo={12} bar={null} />; // error, bar is required |
| 115 | + ~~~~~~~~~~ |
| 116 | +!!! error TS2326: Types of property 'bar' are incompatible. |
| 117 | +!!! error TS2326: Type 'null' is not assignable to type 'ReactNode'. |
| 118 | + |
| 119 | + class JustDefaultProps extends ReactComponent { |
| 120 | + static defaultProps = { |
| 121 | + foo: 42, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + const k = <JustDefaultProps foo={12} />; |
| 126 | + const l = <JustDefaultProps foo={12} bar="ok" />; // error, no prop named bar |
| 127 | + ~~~~~~~~ |
| 128 | +!!! error TS2339: Property 'bar' does not exist on type 'Defaultize<{}, { foo: number; }>'. |
| 129 | + const m = <JustDefaultProps foo="no" />; // error, wrong type |
| 130 | + ~~~~~~~~ |
| 131 | +!!! error TS2326: Types of property 'foo' are incompatible. |
| 132 | +!!! error TS2326: Type 'string' is not assignable to type 'number | undefined'. |
| 133 | + |
| 134 | + interface FooProps { |
| 135 | + foo: string; |
| 136 | + } |
| 137 | + |
| 138 | + class BothWithSpecifiedGeneric extends ReactComponent<FooProps> { |
| 139 | + static propTypes = { |
| 140 | + foo: PropTypes.string, |
| 141 | + bar: PropTypes.node, |
| 142 | + baz: PropTypes.number.isRequired, |
| 143 | + }; |
| 144 | + static defaultProps = { |
| 145 | + foo: "yo", |
| 146 | + }; |
| 147 | + } |
| 148 | + const n = <BothWithSpecifiedGeneric foo="fine" bar="yes" baz={12} />; |
| 149 | + const o = <BothWithSpecifiedGeneric foo="no" />; // Error, missing required prop bar |
| 150 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 151 | +!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'Defaultize<FooProps & InferredPropTypes<{ foo: PropTypeChecker<string, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<number, true>; }>, { foo: string; }>'. |
| 152 | +!!! error TS2322: Type '{ foo: string; }' is not assignable to type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: number; }'. |
| 153 | +!!! error TS2322: Property 'bar' is missing in type '{ foo: string; }'. |
| 154 | + const p = <BothWithSpecifiedGeneric bar="yes" baz={12} />; |
| 155 | + const q = <BothWithSpecifiedGeneric bar="yes" baz={12} bat="ohno" />; // Error, baz not a valid prop |
| 156 | + ~~~~~~~~~~ |
| 157 | +!!! error TS2339: Property 'bat' does not exist on type 'Defaultize<FooProps & InferredPropTypes<{ foo: PropTypeChecker<string, false>; bar: PropTypeChecker<ReactNode, false>; baz: PropTypeChecker<number, true>; }>, { foo: string; }>'. |
| 158 | + const r = <BothWithSpecifiedGeneric foo="no" bar={null} baz={0} />; // bar is nullable/undefinable since it's not marked `isRequired` |
| 159 | + const s = <BothWithSpecifiedGeneric foo="eh" bar="yeah" baz={null} />; // Error, baz is _not_ nullable/undefinable since it's marked `isRequired` |
| 160 | + ~~~~~~~~~~ |
| 161 | +!!! error TS2326: Types of property 'baz' are incompatible. |
| 162 | +!!! error TS2326: Type 'null' is not assignable to type 'number'. |
| 163 | + |
| 164 | + class JustPropTypesWithSpecifiedGeneric extends ReactComponent<FooProps> { |
| 165 | + static propTypes = { |
| 166 | + foo: PropTypes.string, |
| 167 | + bar: PropTypes.node.isRequired, |
| 168 | + }; |
| 169 | + } |
| 170 | + const t = <JustPropTypesWithSpecifiedGeneric foo="nice" bar="ok" />; |
| 171 | + const u = <JustPropTypesWithSpecifiedGeneric foo={12} />; // error, wrong type |
| 172 | + ~~~~~~~~ |
| 173 | +!!! error TS2326: Types of property 'foo' are incompatible. |
| 174 | +!!! error TS2326: Type 'number' is not assignable to type 'string'. |
| 175 | + const v = <JustPropTypesWithSpecifiedGeneric foo={null} bar="ok" />; // generic overrides propTypes required-ness, null isn't valid |
| 176 | + ~~~~~~~~~~ |
| 177 | +!!! error TS2326: Types of property 'foo' are incompatible. |
| 178 | +!!! error TS2326: Type 'null' is not assignable to type 'string'. |
| 179 | + const w = <JustPropTypesWithSpecifiedGeneric foo="cool" bar={null} />; // error, bar is required |
| 180 | + ~~~~~~~~~~ |
| 181 | +!!! error TS2326: Types of property 'bar' are incompatible. |
| 182 | +!!! error TS2326: Type 'null' is not assignable to type 'ReactNode'. |
| 183 | + |
| 184 | + class JustDefaultPropsWithSpecifiedGeneric extends ReactComponent<FooProps> { |
| 185 | + static defaultProps = { |
| 186 | + foo: "no", |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + const x = <JustDefaultPropsWithSpecifiedGeneric foo="eh" />; |
| 191 | + const y = <JustDefaultPropsWithSpecifiedGeneric foo="no" bar="ok" />; // error, no prop named bar |
| 192 | + ~~~~~~~~ |
| 193 | +!!! error TS2339: Property 'bar' does not exist on type 'Defaultize<FooProps, { foo: string; }>'. |
| 194 | + const z = <JustDefaultPropsWithSpecifiedGeneric foo={12} />; // error, wrong type |
| 195 | + ~~~~~~~~ |
| 196 | +!!! error TS2326: Types of property 'foo' are incompatible. |
| 197 | +!!! error TS2326: Type 'number' is not assignable to type 'string | undefined'. |
| 198 | + const aa = <JustDefaultPropsWithSpecifiedGeneric />; |
| 199 | + |
0 commit comments