diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.js b/tests/baselines/reference/isomorphicMappedTypeInference.js new file mode 100644 index 0000000000000..0fc646043dcb7 --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.js @@ -0,0 +1,222 @@ +//// [isomorphicMappedTypeInference.ts] + +type Box = { + value: T; +} + +type Boxified = { + [P in keyof T]: Box; +} + +function box(x: T): Box { + return { value: x }; +} + +function unbox(x: Box): T { + return x.value; +} + +function boxify(obj: T): Boxified { + let result = {} as Boxified; + for (let k in obj) { + result[k] = box(obj[k]); + } + return result; +} + +function unboxify(obj: Boxified): T { + let result = {} as T; + for (let k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} + +function assignBoxified(obj: Boxified, values: T) { + for (let k in values) { + obj[k].value = values[k]; + } +} + +function f1() { + let v = { + a: 42, + b: "hello", + c: true + }; + let b = boxify(v); + let x: number = b.a.value; +} + +function f2() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + let v = unboxify(b); + let x: number = v.a; +} + +function f3() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} + +function f4() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} + +function makeRecord(obj: { [P in K]: T }) { + return obj; +} + +function f5(s: string) { + let b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v.a; +} + +function makeDictionary(obj: { [x: string]: T }) { + return obj; +} + +function f6(s: string) { + let b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v[s]; +} + +//// [isomorphicMappedTypeInference.js] +function box(x) { + return { value: x }; +} +function unbox(x) { + return x.value; +} +function boxify(obj) { + var result = {}; + for (var k in obj) { + result[k] = box(obj[k]); + } + return result; +} +function unboxify(obj) { + var result = {}; + for (var k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} +function assignBoxified(obj, values) { + for (var k in values) { + obj[k].value = values[k]; + } +} +function f1() { + var v = { + a: 42, + b: "hello", + c: true + }; + var b = boxify(v); + var x = b.a.value; +} +function f2() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + var v = unboxify(b); + var x = v.a; +} +function f3() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} +function f4() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} +function makeRecord(obj) { + return obj; +} +function f5(s) { + var b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + var v = unboxify(b); + var x = v.a; +} +function makeDictionary(obj) { + return obj; +} +function f6(s) { + var b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + var v = unboxify(b); + var x = v[s]; +} + + +//// [isomorphicMappedTypeInference.d.ts] +declare type Box = { + value: T; +}; +declare type Boxified = { + [P in keyof T]: Box; +}; +declare function box(x: T): Box; +declare function unbox(x: Box): T; +declare function boxify(obj: T): Boxified; +declare function unboxify(obj: Boxified): T; +declare function assignBoxified(obj: Boxified, values: T): void; +declare function f1(): void; +declare function f2(): void; +declare function f3(): void; +declare function f4(): void; +declare function makeRecord(obj: { + [P in K]: T; +}): { + [P in K]: T; +}; +declare function f5(s: string): void; +declare function makeDictionary(obj: { + [x: string]: T; +}): { + [x: string]: T; +}; +declare function f6(s: string): void; diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.symbols b/tests/baselines/reference/isomorphicMappedTypeInference.symbols new file mode 100644 index 0000000000000..62dcc6316bcf7 --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.symbols @@ -0,0 +1,334 @@ +=== tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts === + +type Box = { +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 1, 9)) + + value: T; +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 1, 9)) +} + +type Boxified = { +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) + + [P in keyof T]: Box; +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 6, 5)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 6, 5)) +} + +function box(x: T): Box { +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 9, 16)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) + + return { value: x }; +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 10, 12)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 9, 16)) +} + +function unbox(x: Box): T { +>unbox : Symbol(unbox, Decl(isomorphicMappedTypeInference.ts, 11, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 13, 18)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) + + return x.value; +>x.value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 13, 18)) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +} + +function boxify(obj: T): Boxified { +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) + + let result = {} as Boxified; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) + + for (let k in obj) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) + + result[k] = box(obj[k]); +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) + } + return result; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +} + +function unboxify(obj: Boxified): T { +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) + + let result = {} as T; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) + + for (let k in obj) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) + + result[k] = unbox(obj[k]); +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) +>unbox : Symbol(unbox, Decl(isomorphicMappedTypeInference.ts, 11, 1)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) + } + return result; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +} + +function assignBoxified(obj: Boxified, values: T) { +>assignBoxified : Symbol(assignBoxified, Decl(isomorphicMappedTypeInference.ts, 31, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 33, 27)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) + + for (let k in values) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) + + obj[k].value = values[k]; +>obj[k].value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 33, 27)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) + } +} + +function f1() { +>f1 : Symbol(f1, Decl(isomorphicMappedTypeInference.ts, 37, 1)) + + let v = { +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 40, 7)) + + a: 42, +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 40, 13)) + + b: "hello", +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 41, 14)) + + c: true +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 42, 19)) + + }; + let b = boxify(v); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 45, 7)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 40, 7)) + + let x: number = b.a.value; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 46, 7)) +>b.a.value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>b.a : Symbol(a) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 45, 7)) +>a : Symbol(a) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +} + +function f2() { +>f2 : Symbol(f2, Decl(isomorphicMappedTypeInference.ts, 47, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 50, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 51, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 52, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 55, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 50, 7)) + + let x: number = v.a; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 56, 7)) +>v.a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 55, 7)) +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +} + +function f3() { +>f3 : Symbol(f3, Decl(isomorphicMappedTypeInference.ts, 57, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 60, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 60, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 61, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 62, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + assignBoxified(b, { c: false }); +>assignBoxified : Symbol(assignBoxified, Decl(isomorphicMappedTypeInference.ts, 31, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 60, 7)) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 65, 23)) +} + +function f4() { +>f4 : Symbol(f4, Decl(isomorphicMappedTypeInference.ts, 66, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 69, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 70, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 71, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + b = boxify(unboxify(b)); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) + + b = unboxify(boxify(b)); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +} + +function makeRecord(obj: { [P in K]: T }) { +>makeRecord : Symbol(makeRecord, Decl(isomorphicMappedTypeInference.ts, 76, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 78, 20)) +>K : Symbol(K, Decl(isomorphicMappedTypeInference.ts, 78, 22)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 78, 41)) +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 78, 49)) +>K : Symbol(K, Decl(isomorphicMappedTypeInference.ts, 78, 22)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 78, 20)) + + return obj; +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 78, 41)) +} + +function f5(s: string) { +>f5 : Symbol(f5, Decl(isomorphicMappedTypeInference.ts, 80, 1)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 82, 12)) + + let b = makeRecord({ +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 83, 7)) +>makeRecord : Symbol(makeRecord, Decl(isomorphicMappedTypeInference.ts, 76, 1)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 83, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 84, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 85, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }); + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 88, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 83, 7)) + + let x: string | number | boolean = v.a; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 89, 7)) +>v.a : Symbol(a) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 88, 7)) +>a : Symbol(a) +} + +function makeDictionary(obj: { [x: string]: T }) { +>makeDictionary : Symbol(makeDictionary, Decl(isomorphicMappedTypeInference.ts, 90, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 92, 24)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 92, 27)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 92, 35)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 92, 24)) + + return obj; +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 92, 27)) +} + +function f6(s: string) { +>f6 : Symbol(f6, Decl(isomorphicMappedTypeInference.ts, 94, 1)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 96, 12)) + + let b = makeDictionary({ +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 97, 7)) +>makeDictionary : Symbol(makeDictionary, Decl(isomorphicMappedTypeInference.ts, 90, 1)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 97, 28)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 98, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 99, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }); + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 102, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 97, 7)) + + let x: string | number | boolean = v[s]; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 103, 7)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 102, 7)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 96, 12)) +} diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.types b/tests/baselines/reference/isomorphicMappedTypeInference.types new file mode 100644 index 0000000000000..00a80639f732c --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.types @@ -0,0 +1,405 @@ +=== tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts === + +type Box = { +>Box : Box +>T : T + + value: T; +>value : T +>T : T +} + +type Boxified = { +>Boxified : Boxified +>T : T + + [P in keyof T]: Box; +>P : P +>T : T +>Box : Box +>T : T +>P : P +} + +function box(x: T): Box { +>box : (x: T) => Box +>T : T +>x : T +>T : T +>Box : Box +>T : T + + return { value: x }; +>{ value: x } : { value: T; } +>value : T +>x : T +} + +function unbox(x: Box): T { +>unbox : (x: Box) => T +>T : T +>x : Box +>Box : Box +>T : T +>T : T + + return x.value; +>x.value : T +>x : Box +>value : T +} + +function boxify(obj: T): Boxified { +>boxify : (obj: T) => Boxified +>T : T +>obj : T +>T : T +>Boxified : Boxified +>T : T + + let result = {} as Boxified; +>result : Boxified +>{} as Boxified : Boxified +>{} : {} +>Boxified : Boxified +>T : T + + for (let k in obj) { +>k : keyof T +>obj : T + + result[k] = box(obj[k]); +>result[k] = box(obj[k]) : Box +>result[k] : Box +>result : Boxified +>k : keyof T +>box(obj[k]) : Box +>box : (x: T) => Box +>obj[k] : T[keyof T] +>obj : T +>k : keyof T + } + return result; +>result : Boxified +} + +function unboxify(obj: Boxified): T { +>unboxify : (obj: Boxified) => T +>T : T +>obj : Boxified +>Boxified : Boxified +>T : T +>T : T + + let result = {} as T; +>result : T +>{} as T : T +>{} : {} +>T : T + + for (let k in obj) { +>k : keyof T +>obj : Boxified + + result[k] = unbox(obj[k]); +>result[k] = unbox(obj[k]) : T[keyof T] +>result[k] : T[keyof T] +>result : T +>k : keyof T +>unbox(obj[k]) : T[keyof T] +>unbox : (x: Box) => T +>obj[k] : Box +>obj : Boxified +>k : keyof T + } + return result; +>result : T +} + +function assignBoxified(obj: Boxified, values: T) { +>assignBoxified : (obj: Boxified, values: T) => void +>T : T +>obj : Boxified +>Boxified : Boxified +>T : T +>values : T +>T : T + + for (let k in values) { +>k : keyof T +>values : T + + obj[k].value = values[k]; +>obj[k].value = values[k] : T[keyof T] +>obj[k].value : T[keyof T] +>obj[k] : Box +>obj : Boxified +>k : keyof T +>value : T[keyof T] +>values[k] : T[keyof T] +>values : T +>k : keyof T + } +} + +function f1() { +>f1 : () => void + + let v = { +>v : { a: number; b: string; c: boolean; } +>{ a: 42, b: "hello", c: true } : { a: number; b: string; c: boolean; } + + a: 42, +>a : number +>42 : 42 + + b: "hello", +>b : string +>"hello" : "hello" + + c: true +>c : boolean +>true : true + + }; + let b = boxify(v); +>b : Boxified<{ a: number; b: string; c: boolean; }> +>boxify(v) : Boxified<{ a: number; b: string; c: boolean; }> +>boxify : (obj: T) => Boxified +>v : { a: number; b: string; c: boolean; } + + let x: number = b.a.value; +>x : number +>b.a.value : number +>b.a : Box +>b : Boxified<{ a: number; b: string; c: boolean; }> +>a : Box +>value : number +} + +function f2() { +>f2 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + let v = unboxify(b); +>v : { a: number; b: string; c: boolean; } +>unboxify(b) : { a: number; b: string; c: boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box; b: Box; c: Box; } + + let x: number = v.a; +>x : number +>v.a : number +>v : { a: number; b: string; c: boolean; } +>a : number +} + +function f3() { +>f3 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + assignBoxified(b, { c: false }); +>assignBoxified(b, { c: false }) : void +>assignBoxified : (obj: Boxified, values: T) => void +>b : { a: Box; b: Box; c: Box; } +>{ c: false } : { c: false; } +>c : boolean +>false : false +} + +function f4() { +>f4 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + b = boxify(unboxify(b)); +>b = boxify(unboxify(b)) : Boxified<{ a: number; b: string; c: boolean; }> +>b : { a: Box; b: Box; c: Box; } +>boxify(unboxify(b)) : Boxified<{ a: number; b: string; c: boolean; }> +>boxify : (obj: T) => Boxified +>unboxify(b) : { a: number; b: string; c: boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box; b: Box; c: Box; } + + b = unboxify(boxify(b)); +>b = unboxify(boxify(b)) : { a: Box; b: Box; c: Box; } +>b : { a: Box; b: Box; c: Box; } +>unboxify(boxify(b)) : { a: Box; b: Box; c: Box; } +>unboxify : (obj: Boxified) => T +>boxify(b) : Boxified<{ a: Box; b: Box; c: Box; }> +>boxify : (obj: T) => Boxified +>b : { a: Box; b: Box; c: Box; } +} + +function makeRecord(obj: { [P in K]: T }) { +>makeRecord : (obj: { [P in K]: T; }) => { [P in K]: T; } +>T : T +>K : K +>obj : { [P in K]: T; } +>P : P +>K : K +>T : T + + return obj; +>obj : { [P in K]: T; } +} + +function f5(s: string) { +>f5 : (s: string) => void +>s : string + + let b = makeRecord({ +>b : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } +>makeRecord({ a: box(42), b: box("hello"), c: box(true) }) : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } +>makeRecord : (obj: { [P in K]: T; }) => { [P in K]: T; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }); + let v = unboxify(b); +>v : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>unboxify(b) : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } + + let x: string | number | boolean = v.a; +>x : string | number | boolean +>v.a : string | number | boolean +>v : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>a : string | number | boolean +} + +function makeDictionary(obj: { [x: string]: T }) { +>makeDictionary : (obj: { [x: string]: T; }) => { [x: string]: T; } +>T : T +>obj : { [x: string]: T; } +>x : string +>T : T + + return obj; +>obj : { [x: string]: T; } +} + +function f6(s: string) { +>f6 : (s: string) => void +>s : string + + let b = makeDictionary({ +>b : { [x: string]: Box | Box | Box; } +>makeDictionary({ a: box(42), b: box("hello"), c: box(true) }) : { [x: string]: Box | Box | Box; } +>makeDictionary : (obj: { [x: string]: T; }) => { [x: string]: T; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }); + let v = unboxify(b); +>v : { [x: string]: string | number | boolean; } +>unboxify(b) : { [x: string]: string | number | boolean; } +>unboxify : (obj: Boxified) => T +>b : { [x: string]: Box | Box | Box; } + + let x: string | number | boolean = v[s]; +>x : string | number | boolean +>v[s] : string | number | boolean +>v : { [x: string]: string | number | boolean; } +>s : string +} diff --git a/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts b/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts new file mode 100644 index 0000000000000..19a0c4889a50f --- /dev/null +++ b/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts @@ -0,0 +1,107 @@ +// @noimplicitany: true +// @declaration: true + +type Box = { + value: T; +} + +type Boxified = { + [P in keyof T]: Box; +} + +function box(x: T): Box { + return { value: x }; +} + +function unbox(x: Box): T { + return x.value; +} + +function boxify(obj: T): Boxified { + let result = {} as Boxified; + for (let k in obj) { + result[k] = box(obj[k]); + } + return result; +} + +function unboxify(obj: Boxified): T { + let result = {} as T; + for (let k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} + +function assignBoxified(obj: Boxified, values: T) { + for (let k in values) { + obj[k].value = values[k]; + } +} + +function f1() { + let v = { + a: 42, + b: "hello", + c: true + }; + let b = boxify(v); + let x: number = b.a.value; +} + +function f2() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + let v = unboxify(b); + let x: number = v.a; +} + +function f3() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} + +function f4() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} + +function makeRecord(obj: { [P in K]: T }) { + return obj; +} + +function f5(s: string) { + let b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v.a; +} + +function makeDictionary(obj: { [x: string]: T }) { + return obj; +} + +function f6(s: string) { + let b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v[s]; +} \ No newline at end of file