diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.js b/tests/baselines/reference/isomorphicMappedTypeInference.js index 0ed2285ed04..560c926877a 100644 --- a/tests/baselines/reference/isomorphicMappedTypeInference.js +++ b/tests/baselines/reference/isomorphicMappedTypeInference.js @@ -143,7 +143,14 @@ var g1 = applySpec({ }); // Infers g2: (...args: any[]) => { foo: { bar: { baz: boolean } } } -var g2 = applySpec({ foo: { bar: { baz: (x: any) => true } } }); +var g2 = applySpec({ foo: { bar: { baz: (x: any) => true } } }); + +// Repro from #12633 + +const foo = (object: T, partial: Partial) => object; +let o = {a: 5, b: 7}; +foo(o, {b: 9}); +o = foo(o, {b: 9}); //// [isomorphicMappedTypeInference.js] function box(x) { @@ -244,6 +251,11 @@ var g1 = applySpec({ }); // Infers g2: (...args: any[]) => { foo: { bar: { baz: boolean } } } var g2 = applySpec({ foo: { bar: { baz: function (x) { return true; } } } }); +// Repro from #12633 +var foo = function (object, partial) { return object; }; +var o = { a: 5, b: 7 }; +foo(o, { b: 9 }); +o = foo(o, { b: 9 }); //// [isomorphicMappedTypeInference.d.ts] @@ -311,3 +323,8 @@ declare var g2: (...args: any[]) => { }; }; }; +declare const foo: (object: T, partial: Partial) => T; +declare let o: { + a: number; + b: number; +}; diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.symbols b/tests/baselines/reference/isomorphicMappedTypeInference.symbols index 92bcbb697ec..93ce825c35d 100644 --- a/tests/baselines/reference/isomorphicMappedTypeInference.symbols +++ b/tests/baselines/reference/isomorphicMappedTypeInference.symbols @@ -459,3 +459,31 @@ var g2 = applySpec({ foo: { bar: { baz: (x: any) => true } } }); >baz : Symbol(baz, Decl(isomorphicMappedTypeInference.ts, 144, 34)) >x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 144, 41)) +// Repro from #12633 + +const foo = (object: T, partial: Partial) => object; +>foo : Symbol(foo, Decl(isomorphicMappedTypeInference.ts, 148, 5)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 148, 13)) +>object : Symbol(object, Decl(isomorphicMappedTypeInference.ts, 148, 16)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 148, 13)) +>partial : Symbol(partial, Decl(isomorphicMappedTypeInference.ts, 148, 26)) +>Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 148, 13)) +>object : Symbol(object, Decl(isomorphicMappedTypeInference.ts, 148, 16)) + +let o = {a: 5, b: 7}; +>o : Symbol(o, Decl(isomorphicMappedTypeInference.ts, 149, 3)) +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 149, 9)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 149, 14)) + +foo(o, {b: 9}); +>foo : Symbol(foo, Decl(isomorphicMappedTypeInference.ts, 148, 5)) +>o : Symbol(o, Decl(isomorphicMappedTypeInference.ts, 149, 3)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 150, 8)) + +o = foo(o, {b: 9}); +>o : Symbol(o, Decl(isomorphicMappedTypeInference.ts, 149, 3)) +>foo : Symbol(foo, Decl(isomorphicMappedTypeInference.ts, 148, 5)) +>o : Symbol(o, Decl(isomorphicMappedTypeInference.ts, 149, 3)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 151, 12)) + diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.types b/tests/baselines/reference/isomorphicMappedTypeInference.types index b4d1383071b..ca20e06e113 100644 --- a/tests/baselines/reference/isomorphicMappedTypeInference.types +++ b/tests/baselines/reference/isomorphicMappedTypeInference.types @@ -546,3 +546,42 @@ var g2 = applySpec({ foo: { bar: { baz: (x: any) => true } } }); >x : any >true : true +// Repro from #12633 + +const foo = (object: T, partial: Partial) => object; +>foo : (object: T, partial: Partial) => T +>(object: T, partial: Partial) => object : (object: T, partial: Partial) => T +>T : T +>object : T +>T : T +>partial : Partial +>Partial : Partial +>T : T +>object : T + +let o = {a: 5, b: 7}; +>o : { a: number; b: number; } +>{a: 5, b: 7} : { a: number; b: number; } +>a : number +>5 : 5 +>b : number +>7 : 7 + +foo(o, {b: 9}); +>foo(o, {b: 9}) : { a: number; b: number; } +>foo : (object: T, partial: Partial) => T +>o : { a: number; b: number; } +>{b: 9} : { b: number; } +>b : number +>9 : 9 + +o = foo(o, {b: 9}); +>o = foo(o, {b: 9}) : { a: number; b: number; } +>o : { a: number; b: number; } +>foo(o, {b: 9}) : { a: number; b: number; } +>foo : (object: T, partial: Partial) => T +>o : { a: number; b: number; } +>{b: 9} : { b: number; } +>b : number +>9 : 9 + diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt index 945652bf929..918c6627f66 100644 --- a/tests/baselines/reference/mappedTypeErrors.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors.errors.txt @@ -20,9 +20,15 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(60,9): error TS2403: Su tests/cases/conformance/types/mapped/mappedTypeErrors.ts(61,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type '{ [P in keyof T]: T[P]; }', but here has type '{ readonly [P in keyof T]: T[P]; }'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(62,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type '{ [P in keyof T]: T[P]; }', but here has type '{ readonly [P in keyof T]?: T[P] | undefined; }'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(67,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type '{ [P in keyof T]: T[P]; }', but here has type '{ [P in keyof T]: T[P][]; }'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(76,45): error TS2345: Argument of type '{ x: number; }' is not assignable to parameter of type 'Readonly<{ x: number; y: number; }>'. + Property 'y' is missing in type '{ x: number; }'. +tests/cases/conformance/types/mapped/mappedTypeErrors.ts(78,59): error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Readonly<{ x: number; y: number; }>'. + 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 (14 errors) ==== +==== tests/cases/conformance/types/mapped/mappedTypeErrors.ts (17 errors) ==== interface Shape { name: string; @@ -126,4 +132,30 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(67,9): error TS2403: Su var x: { [P in keyof T]: T[P][] }; // Error ~ !!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type '{ [P in keyof T]: T[P]; }', but here has type '{ [P in keyof T]: T[P][]; }'. + } + + // Check that inferences to mapped types are secondary + + declare function objAndReadonly(primary: T, secondary: Readonly): T; + declare function objAndPartial(primary: T, secondary: Partial): T; + + function f20() { + let x1 = objAndReadonly({ x: 0, y: 0 }, { x: 1 }); // Error + ~~~~~~~~ +!!! error TS2345: Argument of type '{ x: number; }' is not assignable to parameter of type 'Readonly<{ x: number; y: number; }>'. +!!! error TS2345: Property 'y' is missing in type '{ x: number; }'. + let x2 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1 }); + let x3 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error + ~~~~ +!!! error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'Readonly<{ x: number; y: number; }>'. +!!! error TS2345: Object literal may only specify known properties, and 'z' does not exist in type 'Readonly<{ x: number; y: number; }>'. + } + + 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 + ~~~~ +!!! 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 diff --git a/tests/baselines/reference/mappedTypeErrors.js b/tests/baselines/reference/mappedTypeErrors.js index cb840da32f7..4d2eadbafe9 100644 --- a/tests/baselines/reference/mappedTypeErrors.js +++ b/tests/baselines/reference/mappedTypeErrors.js @@ -66,6 +66,23 @@ function f11() { function f12() { var x: { [P in keyof T]: T[P] }; var x: { [P in keyof T]: T[P][] }; // Error +} + +// Check that inferences to mapped types are secondary + +declare function objAndReadonly(primary: T, secondary: Readonly): T; +declare function objAndPartial(primary: T, secondary: Partial): T; + +function f20() { + let x1 = objAndReadonly({ x: 0, y: 0 }, { x: 1 }); // Error + let x2 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1 }); + let x3 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error +} + +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 } //// [mappedTypeErrors.js] @@ -97,6 +114,16 @@ function f12() { var x; var x; // Error } +function f20() { + var x1 = objAndReadonly({ x: 0, y: 0 }, { x: 1 }); // Error + var x2 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1 }); + var x3 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error +} +function f21() { + var x1 = objAndPartial({ x: 0, y: 0 }, { x: 1 }); + 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 +} //// [mappedTypeErrors.d.ts] @@ -137,3 +164,7 @@ declare function f4(x: T): void; declare function f10(): void; declare function f11(): void; declare function f12(): void; +declare function objAndReadonly(primary: T, secondary: Readonly): T; +declare function objAndPartial(primary: T, secondary: Partial): T; +declare function f20(): void; +declare function f21(): void;