diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt index 918c6627f6..17c77fa50a 100644 --- a/tests/baselines/reference/mappedTypeErrors.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors.errors.txt @@ -26,9 +26,19 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(78,59): error TS2345: A Object literal may only specify known properties, and 'z' does not exist in type 'Readonly<{ x: number; y: number; }>'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(84,58): error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Partial<{ x: number; y: number; }>'. Object literal may only specify known properties, and 'z' does not exist in type 'Partial<{ x: number; y: number; }>'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(106,15): error TS2345: Argument of type '{ a: undefined; }' is not assignable to parameter of type 'Pick'. + Types of property 'a' are incompatible. + Type 'undefined' is not assignable to type 'string'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(107,17): error TS2345: Argument of type '{ c: boolean; }' is not assignable to parameter of type 'Pick'. + Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(124,12): error TS2345: Argument of type '{ a: undefined; }' is not assignable to parameter of type 'Pick'. + Types of property 'a' are incompatible. + Type 'undefined' is not assignable to type 'string'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(125,14): error TS2345: Argument of type '{ c: boolean; }' is not assignable to parameter of type 'Pick'. + Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. -==== tests/cases/conformance/types/mapped/mappedTypeErrors.ts (17 errors) ==== +==== tests/cases/conformance/types/mapped/mappedTypeErrors.ts (21 errors) ==== interface Shape { name: string; @@ -158,4 +168,59 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(84,58): error TS2345: A ~~~~ !!! error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Partial<{ x: number; y: number; }>'. !!! error TS2345: Object literal may only specify known properties, and 'z' does not exist in type 'Partial<{ x: number; y: number; }>'. - } \ No newline at end of file + } + + // Verify use of Pick for setState functions (#12793) + + interface Foo { + a: string; + b?: number; + } + + function setState(obj: T, props: Pick) { + for (let k in props) { + obj[k] = props[k]; + } + } + + let foo: Foo = { a: "hello", b: 42 }; + setState(foo, { a: "test", b: 43 }) + setState(foo, { a: "hi" }); + setState(foo, { b: undefined }); + setState(foo, { }); + setState(foo, foo); + setState(foo, { a: undefined }); // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ a: undefined; }' is not assignable to parameter of type 'Pick'. +!!! error TS2345: Types of property 'a' are incompatible. +!!! error TS2345: Type 'undefined' is not assignable to type 'string'. + setState(foo, { c: true }); // Error + ~~~~~~~ +!!! error TS2345: Argument of type '{ c: boolean; }' is not assignable to parameter of type 'Pick'. +!!! error TS2345: Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. + + class C { + state: T; + setState(props: Pick) { + for (let k in props) { + this.state[k] = props[k]; + } + } + } + + let c = new C(); + c.setState({ a: "test", b: 43 }); + c.setState({ a: "hi" }); + c.setState({ b: undefined }); + c.setState({ }); + c.setState(foo); + c.setState({ a: undefined }); // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ a: undefined; }' is not assignable to parameter of type 'Pick'. +!!! error TS2345: Types of property 'a' are incompatible. +!!! error TS2345: Type 'undefined' is not assignable to type 'string'. + c.setState({ c: true }); // Error + ~~~~~~~ +!!! error TS2345: Argument of type '{ c: boolean; }' is not assignable to parameter of type 'Pick'. +!!! error TS2345: Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. + \ No newline at end of file diff --git a/tests/baselines/reference/mappedTypeErrors.js b/tests/baselines/reference/mappedTypeErrors.js index 4d2eadbafe..e1b458565b 100644 --- a/tests/baselines/reference/mappedTypeErrors.js +++ b/tests/baselines/reference/mappedTypeErrors.js @@ -83,7 +83,48 @@ function f21() { let x1 = objAndPartial({ x: 0, y: 0 }, { x: 1 }); let x2 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1 }); let x3 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error -} +} + +// Verify use of Pick for setState functions (#12793) + +interface Foo { + a: string; + b?: number; +} + +function setState(obj: T, props: Pick) { + for (let k in props) { + obj[k] = props[k]; + } +} + +let foo: Foo = { a: "hello", b: 42 }; +setState(foo, { a: "test", b: 43 }) +setState(foo, { a: "hi" }); +setState(foo, { b: undefined }); +setState(foo, { }); +setState(foo, foo); +setState(foo, { a: undefined }); // Error +setState(foo, { c: true }); // Error + +class C { + state: T; + setState(props: Pick) { + for (let k in props) { + this.state[k] = props[k]; + } + } +} + +let c = new C(); +c.setState({ a: "test", b: 43 }); +c.setState({ a: "hi" }); +c.setState({ b: undefined }); +c.setState({ }); +c.setState(foo); +c.setState({ a: undefined }); // Error +c.setState({ c: true }); // Error + //// [mappedTypeErrors.js] function f1(x) { @@ -124,6 +165,37 @@ function f21() { var x2 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1 }); var x3 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error } +function setState(obj, props) { + for (var k in props) { + obj[k] = props[k]; + } +} +var foo = { a: "hello", b: 42 }; +setState(foo, { a: "test", b: 43 }); +setState(foo, { a: "hi" }); +setState(foo, { b: undefined }); +setState(foo, {}); +setState(foo, foo); +setState(foo, { a: undefined }); // Error +setState(foo, { c: true }); // Error +var C = (function () { + function C() { + } + C.prototype.setState = function (props) { + for (var k in props) { + this.state[k] = props[k]; + } + }; + return C; +}()); +var c = new C(); +c.setState({ a: "test", b: 43 }); +c.setState({ a: "hi" }); +c.setState({ b: undefined }); +c.setState({}); +c.setState(foo); +c.setState({ a: undefined }); // Error +c.setState({ c: true }); // Error //// [mappedTypeErrors.d.ts] @@ -168,3 +240,14 @@ declare function objAndReadonly(primary: T, secondary: Readonly): T; declare function objAndPartial(primary: T, secondary: Partial): T; declare function f20(): void; declare function f21(): void; +interface Foo { + a: string; + b?: number; +} +declare function setState(obj: T, props: Pick): void; +declare let foo: Foo; +declare class C { + state: T; + setState(props: Pick): void; +} +declare let c: C; diff --git a/tests/cases/conformance/types/mapped/mappedTypeErrors.ts b/tests/cases/conformance/types/mapped/mappedTypeErrors.ts index a198d4e0dc..4be6b6b098 100644 --- a/tests/cases/conformance/types/mapped/mappedTypeErrors.ts +++ b/tests/cases/conformance/types/mapped/mappedTypeErrors.ts @@ -84,4 +84,44 @@ function f21() { let x1 = objAndPartial({ x: 0, y: 0 }, { x: 1 }); let x2 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1 }); let x3 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error -} \ No newline at end of file +} + +// Verify use of Pick for setState functions (#12793) + +interface Foo { + a: string; + b?: number; +} + +function setState(obj: T, props: Pick) { + for (let k in props) { + obj[k] = props[k]; + } +} + +let foo: Foo = { a: "hello", b: 42 }; +setState(foo, { a: "test", b: 43 }) +setState(foo, { a: "hi" }); +setState(foo, { b: undefined }); +setState(foo, { }); +setState(foo, foo); +setState(foo, { a: undefined }); // Error +setState(foo, { c: true }); // Error + +class C { + state: T; + setState(props: Pick) { + for (let k in props) { + this.state[k] = props[k]; + } + } +} + +let c = new C(); +c.setState({ a: "test", b: 43 }); +c.setState({ a: "hi" }); +c.setState({ b: undefined }); +c.setState({ }); +c.setState(foo); +c.setState({ a: undefined }); // Error +c.setState({ c: true }); // Error