diff --git a/tests/baselines/reference/mappedTypes1.js b/tests/baselines/reference/mappedTypes1.js new file mode 100644 index 00000000000..df03a022966 --- /dev/null +++ b/tests/baselines/reference/mappedTypes1.js @@ -0,0 +1,146 @@ +//// [mappedTypes1.ts] + +type Item = { a: string, b: number, c: boolean }; + +type T00 = { [P in "x" | "y"]: number }; +type T01 = { [P in "x" | "y"]: P }; +type T02 = { [P in "a" | "b"]: Item[P]; } +type T03 = { [P in keyof Item]: Date }; + +type T10 = { [P in keyof Item]: Item[P] }; +type T11 = { [P in keyof Item]?: Item[P] }; +type T12 = { readonly [P in keyof Item]: Item[P] }; +type T13 = { readonly [P in keyof Item]?: Item[P] }; + +type T20 = { [P in keyof Item]: Item[P] | null }; +type T21 = { [P in keyof Item]: Array }; + +type T30 = { [P in keyof any]: void }; +type T31 = { [P in keyof string]: void }; +type T32 = { [P in keyof number]: void }; +type T33 = { [P in keyof boolean]: void }; +type T34 = { [P in keyof undefined]: void }; +type T35 = { [P in keyof null]: void }; +type T36 = { [P in keyof void]: void }; +type T37 = { [P in keyof symbol]: void }; +type T38 = { [P in keyof never]: void }; + +declare function f1(): { [P in keyof T1]: void }; +declare function f2(): { [P in keyof T1]: void }; +declare function f3(): { [P in keyof T1]: void }; + +let x1 = f1(); +let x2 = f2(); +let x3 = f3(); + +//// [mappedTypes1.js] +var x1 = f1(); +var x2 = f2(); +var x3 = f3(); + + +//// [mappedTypes1.d.ts] +declare type Item = { + a: string; + b: number; + c: boolean; +}; +declare type T00 = { + [P in "x" | "y"]: number; +}; +declare type T01 = { + [P in "x" | "y"]: P; +}; +declare type T02 = { + [P in "a" | "b"]: Item[P]; +}; +declare type T03 = { + [P in keyof Item]: Date; +}; +declare type T10 = { + [P in keyof Item]: Item[P]; +}; +declare type T11 = { + [P in keyof Item]?: Item[P]; +}; +declare type T12 = { + readonly [P in keyof Item]: Item[P]; +}; +declare type T13 = { + readonly [P in keyof Item]?: Item[P]; +}; +declare type T20 = { + [P in keyof Item]: Item[P] | null; +}; +declare type T21 = { + [P in keyof Item]: Array; +}; +declare type T30 = { + [P in keyof any]: void; +}; +declare type T31 = { + [P in keyof string]: void; +}; +declare type T32 = { + [P in keyof number]: void; +}; +declare type T33 = { + [P in keyof boolean]: void; +}; +declare type T34 = { + [P in keyof undefined]: void; +}; +declare type T35 = { + [P in keyof null]: void; +}; +declare type T36 = { + [P in keyof void]: void; +}; +declare type T37 = { + [P in keyof symbol]: void; +}; +declare type T38 = { + [P in keyof never]: void; +}; +declare function f1(): { + [P in keyof T1]: void; +}; +declare function f2(): { + [P in keyof T1]: void; +}; +declare function f3(): { + [P in keyof T1]: void; +}; +declare let x1: {}; +declare let x2: { + [x: number]: void; + toString: void; + charAt: void; + charCodeAt: void; + concat: void; + indexOf: void; + lastIndexOf: void; + localeCompare: void; + match: void; + replace: void; + search: void; + slice: void; + split: void; + substring: void; + toLowerCase: void; + toLocaleLowerCase: void; + toUpperCase: void; + toLocaleUpperCase: void; + trim: void; + length: void; + substr: void; + valueOf: void; +}; +declare let x3: { + toString: void; + valueOf: void; + toFixed: void; + toExponential: void; + toPrecision: void; + toLocaleString: void; +}; diff --git a/tests/baselines/reference/mappedTypes1.symbols b/tests/baselines/reference/mappedTypes1.symbols new file mode 100644 index 00000000000..65732a4be11 --- /dev/null +++ b/tests/baselines/reference/mappedTypes1.symbols @@ -0,0 +1,138 @@ +=== tests/cases/conformance/types/mapped/mappedTypes1.ts === + +type Item = { a: string, b: number, c: boolean }; +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>a : Symbol(a, Decl(mappedTypes1.ts, 1, 13)) +>b : Symbol(b, Decl(mappedTypes1.ts, 1, 24)) +>c : Symbol(c, Decl(mappedTypes1.ts, 1, 35)) + +type T00 = { [P in "x" | "y"]: number }; +>T00 : Symbol(T00, Decl(mappedTypes1.ts, 1, 49)) +>P : Symbol(P, Decl(mappedTypes1.ts, 3, 14)) + +type T01 = { [P in "x" | "y"]: P }; +>T01 : Symbol(T01, Decl(mappedTypes1.ts, 3, 40)) +>P : Symbol(P, Decl(mappedTypes1.ts, 4, 14)) +>P : Symbol(P, Decl(mappedTypes1.ts, 4, 14)) + +type T02 = { [P in "a" | "b"]: Item[P]; } +>T02 : Symbol(T02, Decl(mappedTypes1.ts, 4, 35)) +>P : Symbol(P, Decl(mappedTypes1.ts, 5, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 5, 14)) + +type T03 = { [P in keyof Item]: Date }; +>T03 : Symbol(T03, Decl(mappedTypes1.ts, 5, 41)) +>P : Symbol(P, Decl(mappedTypes1.ts, 6, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Date : Symbol(Date, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + +type T10 = { [P in keyof Item]: Item[P] }; +>T10 : Symbol(T10, Decl(mappedTypes1.ts, 6, 39)) +>P : Symbol(P, Decl(mappedTypes1.ts, 8, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 8, 14)) + +type T11 = { [P in keyof Item]?: Item[P] }; +>T11 : Symbol(T11, Decl(mappedTypes1.ts, 8, 42)) +>P : Symbol(P, Decl(mappedTypes1.ts, 9, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 9, 14)) + +type T12 = { readonly [P in keyof Item]: Item[P] }; +>T12 : Symbol(T12, Decl(mappedTypes1.ts, 9, 43)) +>P : Symbol(P, Decl(mappedTypes1.ts, 10, 23)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 10, 23)) + +type T13 = { readonly [P in keyof Item]?: Item[P] }; +>T13 : Symbol(T13, Decl(mappedTypes1.ts, 10, 51)) +>P : Symbol(P, Decl(mappedTypes1.ts, 11, 23)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 11, 23)) + +type T20 = { [P in keyof Item]: Item[P] | null }; +>T20 : Symbol(T20, Decl(mappedTypes1.ts, 11, 52)) +>P : Symbol(P, Decl(mappedTypes1.ts, 13, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 13, 14)) + +type T21 = { [P in keyof Item]: Array }; +>T21 : Symbol(T21, Decl(mappedTypes1.ts, 13, 49)) +>P : Symbol(P, Decl(mappedTypes1.ts, 14, 14)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>Item : Symbol(Item, Decl(mappedTypes1.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes1.ts, 14, 14)) + +type T30 = { [P in keyof any]: void }; +>T30 : Symbol(T30, Decl(mappedTypes1.ts, 14, 49)) +>P : Symbol(P, Decl(mappedTypes1.ts, 16, 14)) + +type T31 = { [P in keyof string]: void }; +>T31 : Symbol(T31, Decl(mappedTypes1.ts, 16, 38)) +>P : Symbol(P, Decl(mappedTypes1.ts, 17, 14)) + +type T32 = { [P in keyof number]: void }; +>T32 : Symbol(T32, Decl(mappedTypes1.ts, 17, 41)) +>P : Symbol(P, Decl(mappedTypes1.ts, 18, 14)) + +type T33 = { [P in keyof boolean]: void }; +>T33 : Symbol(T33, Decl(mappedTypes1.ts, 18, 41)) +>P : Symbol(P, Decl(mappedTypes1.ts, 19, 14)) + +type T34 = { [P in keyof undefined]: void }; +>T34 : Symbol(T34, Decl(mappedTypes1.ts, 19, 42)) +>P : Symbol(P, Decl(mappedTypes1.ts, 20, 14)) + +type T35 = { [P in keyof null]: void }; +>T35 : Symbol(T35, Decl(mappedTypes1.ts, 20, 44)) +>P : Symbol(P, Decl(mappedTypes1.ts, 21, 14)) + +type T36 = { [P in keyof void]: void }; +>T36 : Symbol(T36, Decl(mappedTypes1.ts, 21, 39)) +>P : Symbol(P, Decl(mappedTypes1.ts, 22, 14)) + +type T37 = { [P in keyof symbol]: void }; +>T37 : Symbol(T37, Decl(mappedTypes1.ts, 22, 39)) +>P : Symbol(P, Decl(mappedTypes1.ts, 23, 14)) + +type T38 = { [P in keyof never]: void }; +>T38 : Symbol(T38, Decl(mappedTypes1.ts, 23, 41)) +>P : Symbol(P, Decl(mappedTypes1.ts, 24, 14)) + +declare function f1(): { [P in keyof T1]: void }; +>f1 : Symbol(f1, Decl(mappedTypes1.ts, 24, 40)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 26, 20)) +>P : Symbol(P, Decl(mappedTypes1.ts, 26, 30)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 26, 20)) + +declare function f2(): { [P in keyof T1]: void }; +>f2 : Symbol(f2, Decl(mappedTypes1.ts, 26, 53)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 27, 20)) +>P : Symbol(P, Decl(mappedTypes1.ts, 27, 45)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 27, 20)) + +declare function f3(): { [P in keyof T1]: void }; +>f3 : Symbol(f3, Decl(mappedTypes1.ts, 27, 68)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 28, 20)) +>P : Symbol(P, Decl(mappedTypes1.ts, 28, 45)) +>T1 : Symbol(T1, Decl(mappedTypes1.ts, 28, 20)) + +let x1 = f1(); +>x1 : Symbol(x1, Decl(mappedTypes1.ts, 30, 3)) +>f1 : Symbol(f1, Decl(mappedTypes1.ts, 24, 40)) + +let x2 = f2(); +>x2 : Symbol(x2, Decl(mappedTypes1.ts, 31, 3)) +>f2 : Symbol(f2, Decl(mappedTypes1.ts, 26, 53)) + +let x3 = f3(); +>x3 : Symbol(x3, Decl(mappedTypes1.ts, 32, 3)) +>f3 : Symbol(f3, Decl(mappedTypes1.ts, 27, 68)) + diff --git a/tests/baselines/reference/mappedTypes1.types b/tests/baselines/reference/mappedTypes1.types new file mode 100644 index 00000000000..222291b008b --- /dev/null +++ b/tests/baselines/reference/mappedTypes1.types @@ -0,0 +1,143 @@ +=== tests/cases/conformance/types/mapped/mappedTypes1.ts === + +type Item = { a: string, b: number, c: boolean }; +>Item : Item +>a : string +>b : number +>c : boolean + +type T00 = { [P in "x" | "y"]: number }; +>T00 : T00 +>P : P + +type T01 = { [P in "x" | "y"]: P }; +>T01 : T01 +>P : P +>P : P + +type T02 = { [P in "a" | "b"]: Item[P]; } +>T02 : T02 +>P : P +>Item : Item +>P : P + +type T03 = { [P in keyof Item]: Date }; +>T03 : T03 +>P : P +>Item : Item +>Date : Date + +type T10 = { [P in keyof Item]: Item[P] }; +>T10 : T10 +>P : P +>Item : Item +>Item : Item +>P : P + +type T11 = { [P in keyof Item]?: Item[P] }; +>T11 : T11 +>P : P +>Item : Item +>Item : Item +>P : P + +type T12 = { readonly [P in keyof Item]: Item[P] }; +>T12 : T12 +>P : P +>Item : Item +>Item : Item +>P : P + +type T13 = { readonly [P in keyof Item]?: Item[P] }; +>T13 : T13 +>P : P +>Item : Item +>Item : Item +>P : P + +type T20 = { [P in keyof Item]: Item[P] | null }; +>T20 : T20 +>P : P +>Item : Item +>Item : Item +>P : P +>null : null + +type T21 = { [P in keyof Item]: Array }; +>T21 : T21 +>P : P +>Item : Item +>Array : T[] +>Item : Item +>P : P + +type T30 = { [P in keyof any]: void }; +>T30 : T30 +>P : P + +type T31 = { [P in keyof string]: void }; +>T31 : T31 +>P : P + +type T32 = { [P in keyof number]: void }; +>T32 : T32 +>P : P + +type T33 = { [P in keyof boolean]: void }; +>T33 : T33 +>P : P + +type T34 = { [P in keyof undefined]: void }; +>T34 : T34 +>P : P + +type T35 = { [P in keyof null]: void }; +>T35 : T35 +>P : P +>null : null + +type T36 = { [P in keyof void]: void }; +>T36 : T36 +>P : P + +type T37 = { [P in keyof symbol]: void }; +>T37 : T37 +>P : P + +type T38 = { [P in keyof never]: void }; +>T38 : T38 +>P : P + +declare function f1(): { [P in keyof T1]: void }; +>f1 : () => { [P in keyof T1]: void; } +>T1 : T1 +>P : P +>T1 : T1 + +declare function f2(): { [P in keyof T1]: void }; +>f2 : () => { [P in keyof T1]: void; } +>T1 : T1 +>P : P +>T1 : T1 + +declare function f3(): { [P in keyof T1]: void }; +>f3 : () => { [P in keyof T1]: void; } +>T1 : T1 +>P : P +>T1 : T1 + +let x1 = f1(); +>x1 : {} +>f1() : {} +>f1 : () => { [P in keyof T1]: void; } + +let x2 = f2(); +>x2 : { [x: number]: void; toString: void; charAt: void; charCodeAt: void; concat: void; indexOf: void; lastIndexOf: void; localeCompare: void; match: void; replace: void; search: void; slice: void; split: void; substring: void; toLowerCase: void; toLocaleLowerCase: void; toUpperCase: void; toLocaleUpperCase: void; trim: void; length: void; substr: void; valueOf: void; } +>f2() : { [x: number]: void; toString: void; charAt: void; charCodeAt: void; concat: void; indexOf: void; lastIndexOf: void; localeCompare: void; match: void; replace: void; search: void; slice: void; split: void; substring: void; toLowerCase: void; toLocaleLowerCase: void; toUpperCase: void; toLocaleUpperCase: void; trim: void; length: void; substr: void; valueOf: void; } +>f2 : () => { [P in keyof T1]: void; } + +let x3 = f3(); +>x3 : { toString: void; valueOf: void; toFixed: void; toExponential: void; toPrecision: void; toLocaleString: void; } +>f3() : { toString: void; valueOf: void; toFixed: void; toExponential: void; toPrecision: void; toLocaleString: void; } +>f3 : () => { [P in keyof T1]: void; } + diff --git a/tests/baselines/reference/mappedTypes2.js b/tests/baselines/reference/mappedTypes2.js new file mode 100644 index 00000000000..8796244c390 --- /dev/null +++ b/tests/baselines/reference/mappedTypes2.js @@ -0,0 +1,183 @@ +//// [mappedTypes2.ts] + +type Partial = { + [P in keyof T]?: T[P]; +}; + +type Readonly = { + readonly [P in keyof T]: T[P]; +}; + +type Pick = { + [P in K]: T[P]; +} + +type Record = { + [_ in K]: T; +} + +type Proxy = { + get(): T; + set(value: T): void; +} + +type Proxify = { + [P in keyof T]: Proxy; +} + +type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; + +declare function assign(obj: T, props: Partial): void; +declare function freeze(obj: T): Readonly; +declare function pick(obj: T, ...keys: K[]): Pick; +declare function mapObject(obj: Record, f: (x: T) => U): Record; +declare function proxify(obj: T): Proxify; + +interface Shape { + name: string; + width: number; + height: number; + visible: boolean; +} + +interface PartialShape { + name?: string; + width?: number; + height?: number; + visible?: boolean; +} + +interface ReadonlyShape { + readonly name: string; + readonly width: number; + readonly height: number; + readonly visible: boolean; +} + +function f0(s1: Shape, s2: Shape) { + assign(s1, { name: "circle" }); + assign(s2, { width: 10, height: 20 }); +} + +function f1(shape: Shape) { + var frozen: ReadonlyShape; + var frozen: Readonly; + var frozen = freeze(shape); +} + +function f2(shape: Shape) { + var partial: PartialShape; + var partial: Partial; + var partial: Partial = {}; +} + +function f3(shape: Shape) { + const x = pick(shape, "name", "visible"); // { name: string, visible: boolean } +} + +function f4() { + const rec = { foo: "hello", bar: "world", baz: "bye" }; + const lengths = mapObject(rec, s => s.length); // { foo: number, bar: number, baz: number } +} + +function f5(shape: Shape) { + const p = proxify(shape); + let name = p.name.get(); + p.visible.set(false); +} + +function f6(shape: DeepReadonly) { + let name = shape.name; // DeepReadonly + let length = name.length; // DeepReadonly + let toString = length.toString; // DeepReadonly<(radix?: number) => string> +} + +//// [mappedTypes2.js] +function f0(s1, s2) { + assign(s1, { name: "circle" }); + assign(s2, { width: 10, height: 20 }); +} +function f1(shape) { + var frozen; + var frozen; + var frozen = freeze(shape); +} +function f2(shape) { + var partial; + var partial; + var partial = {}; +} +function f3(shape) { + var x = pick(shape, "name", "visible"); // { name: string, visible: boolean } +} +function f4() { + var rec = { foo: "hello", bar: "world", baz: "bye" }; + var lengths = mapObject(rec, function (s) { return s.length; }); // { foo: number, bar: number, baz: number } +} +function f5(shape) { + var p = proxify(shape); + var name = p.name.get(); + p.visible.set(false); +} +function f6(shape) { + var name = shape.name; // DeepReadonly + var length = name.length; // DeepReadonly + var toString = length.toString; // DeepReadonly<(radix?: number) => string> +} + + +//// [mappedTypes2.d.ts] +declare type Partial = { + [P in keyof T]?: T[P]; +}; +declare type Readonly = { + readonly [P in keyof T]: T[P]; +}; +declare type Pick = { + [P in K]: T[P]; +}; +declare type Record = { + [_ in K]: T; +}; +declare type Proxy = { + get(): T; + set(value: T): void; +}; +declare type Proxify = { + [P in keyof T]: Proxy; +}; +declare type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; +declare function assign(obj: T, props: Partial): void; +declare function freeze(obj: T): Readonly; +declare function pick(obj: T, ...keys: K[]): Pick; +declare function mapObject(obj: Record, f: (x: T) => U): Record; +declare function proxify(obj: T): Proxify; +interface Shape { + name: string; + width: number; + height: number; + visible: boolean; +} +interface PartialShape { + name?: string; + width?: number; + height?: number; + visible?: boolean; +} +interface ReadonlyShape { + readonly name: string; + readonly width: number; + readonly height: number; + readonly visible: boolean; +} +declare function f0(s1: Shape, s2: Shape): void; +declare function f1(shape: Shape): void; +declare function f2(shape: Shape): void; +declare function f3(shape: Shape): void; +declare function f4(): void; +declare function f5(shape: Shape): void; +declare function f6(shape: DeepReadonly): void; diff --git a/tests/baselines/reference/mappedTypes2.symbols b/tests/baselines/reference/mappedTypes2.symbols new file mode 100644 index 00000000000..bc823ed0081 --- /dev/null +++ b/tests/baselines/reference/mappedTypes2.symbols @@ -0,0 +1,331 @@ +=== tests/cases/conformance/types/mapped/mappedTypes2.ts === + +type Partial = { +>Partial : Symbol(Partial, Decl(mappedTypes2.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes2.ts, 1, 13)) + + [P in keyof T]?: T[P]; +>P : Symbol(P, Decl(mappedTypes2.ts, 2, 5)) +>T : Symbol(T, Decl(mappedTypes2.ts, 1, 13)) +>T : Symbol(T, Decl(mappedTypes2.ts, 1, 13)) +>P : Symbol(P, Decl(mappedTypes2.ts, 2, 5)) + +}; + +type Readonly = { +>Readonly : Symbol(Readonly, Decl(mappedTypes2.ts, 3, 2)) +>T : Symbol(T, Decl(mappedTypes2.ts, 5, 14)) + + readonly [P in keyof T]: T[P]; +>P : Symbol(P, Decl(mappedTypes2.ts, 6, 14)) +>T : Symbol(T, Decl(mappedTypes2.ts, 5, 14)) +>T : Symbol(T, Decl(mappedTypes2.ts, 5, 14)) +>P : Symbol(P, Decl(mappedTypes2.ts, 6, 14)) + +}; + +type Pick = { +>Pick : Symbol(Pick, Decl(mappedTypes2.ts, 7, 2)) +>T : Symbol(T, Decl(mappedTypes2.ts, 9, 10)) +>K : Symbol(K, Decl(mappedTypes2.ts, 9, 12)) +>T : Symbol(T, Decl(mappedTypes2.ts, 9, 10)) + + [P in K]: T[P]; +>P : Symbol(P, Decl(mappedTypes2.ts, 10, 5)) +>K : Symbol(K, Decl(mappedTypes2.ts, 9, 12)) +>T : Symbol(T, Decl(mappedTypes2.ts, 9, 10)) +>P : Symbol(P, Decl(mappedTypes2.ts, 10, 5)) +} + +type Record = { +>Record : Symbol(Record, Decl(mappedTypes2.ts, 11, 1)) +>K : Symbol(K, Decl(mappedTypes2.ts, 13, 12)) +>T : Symbol(T, Decl(mappedTypes2.ts, 13, 38)) + + [_ in K]: T; +>_ : Symbol(_, Decl(mappedTypes2.ts, 14, 5)) +>K : Symbol(K, Decl(mappedTypes2.ts, 13, 12)) +>T : Symbol(T, Decl(mappedTypes2.ts, 13, 38)) +} + +type Proxy = { +>Proxy : Symbol(Proxy, Decl(mappedTypes2.ts, 15, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 17, 11)) + + get(): T; +>get : Symbol(get, Decl(mappedTypes2.ts, 17, 17)) +>T : Symbol(T, Decl(mappedTypes2.ts, 17, 11)) + + set(value: T): void; +>set : Symbol(set, Decl(mappedTypes2.ts, 18, 13)) +>value : Symbol(value, Decl(mappedTypes2.ts, 19, 8)) +>T : Symbol(T, Decl(mappedTypes2.ts, 17, 11)) +} + +type Proxify = { +>Proxify : Symbol(Proxify, Decl(mappedTypes2.ts, 20, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 22, 13)) + + [P in keyof T]: Proxy; +>P : Symbol(P, Decl(mappedTypes2.ts, 23, 5)) +>T : Symbol(T, Decl(mappedTypes2.ts, 22, 13)) +>Proxy : Symbol(Proxy, Decl(mappedTypes2.ts, 15, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 22, 13)) +>P : Symbol(P, Decl(mappedTypes2.ts, 23, 5)) +} + +type DeepReadonly = { +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes2.ts, 24, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 26, 18)) + + readonly [P in keyof T]: DeepReadonly; +>P : Symbol(P, Decl(mappedTypes2.ts, 27, 14)) +>T : Symbol(T, Decl(mappedTypes2.ts, 26, 18)) +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes2.ts, 24, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 26, 18)) +>P : Symbol(P, Decl(mappedTypes2.ts, 27, 14)) + +}; + +declare function assign(obj: T, props: Partial): void; +>assign : Symbol(assign, Decl(mappedTypes2.ts, 28, 2)) +>T : Symbol(T, Decl(mappedTypes2.ts, 30, 24)) +>obj : Symbol(obj, Decl(mappedTypes2.ts, 30, 27)) +>T : Symbol(T, Decl(mappedTypes2.ts, 30, 24)) +>props : Symbol(props, Decl(mappedTypes2.ts, 30, 34)) +>Partial : Symbol(Partial, Decl(mappedTypes2.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes2.ts, 30, 24)) + +declare function freeze(obj: T): Readonly; +>freeze : Symbol(freeze, Decl(mappedTypes2.ts, 30, 60)) +>T : Symbol(T, Decl(mappedTypes2.ts, 31, 24)) +>obj : Symbol(obj, Decl(mappedTypes2.ts, 31, 27)) +>T : Symbol(T, Decl(mappedTypes2.ts, 31, 24)) +>Readonly : Symbol(Readonly, Decl(mappedTypes2.ts, 3, 2)) +>T : Symbol(T, Decl(mappedTypes2.ts, 31, 24)) + +declare function pick(obj: T, ...keys: K[]): Pick; +>pick : Symbol(pick, Decl(mappedTypes2.ts, 31, 48)) +>T : Symbol(T, Decl(mappedTypes2.ts, 32, 22)) +>K : Symbol(K, Decl(mappedTypes2.ts, 32, 24)) +>T : Symbol(T, Decl(mappedTypes2.ts, 32, 22)) +>obj : Symbol(obj, Decl(mappedTypes2.ts, 32, 44)) +>T : Symbol(T, Decl(mappedTypes2.ts, 32, 22)) +>keys : Symbol(keys, Decl(mappedTypes2.ts, 32, 51)) +>K : Symbol(K, Decl(mappedTypes2.ts, 32, 24)) +>Pick : Symbol(Pick, Decl(mappedTypes2.ts, 7, 2)) +>T : Symbol(T, Decl(mappedTypes2.ts, 32, 22)) +>K : Symbol(K, Decl(mappedTypes2.ts, 32, 24)) + +declare function mapObject(obj: Record, f: (x: T) => U): Record; +>mapObject : Symbol(mapObject, Decl(mappedTypes2.ts, 32, 78)) +>K : Symbol(K, Decl(mappedTypes2.ts, 33, 27)) +>T : Symbol(T, Decl(mappedTypes2.ts, 33, 53)) +>U : Symbol(U, Decl(mappedTypes2.ts, 33, 56)) +>obj : Symbol(obj, Decl(mappedTypes2.ts, 33, 60)) +>Record : Symbol(Record, Decl(mappedTypes2.ts, 11, 1)) +>K : Symbol(K, Decl(mappedTypes2.ts, 33, 27)) +>T : Symbol(T, Decl(mappedTypes2.ts, 33, 53)) +>f : Symbol(f, Decl(mappedTypes2.ts, 33, 78)) +>x : Symbol(x, Decl(mappedTypes2.ts, 33, 83)) +>T : Symbol(T, Decl(mappedTypes2.ts, 33, 53)) +>U : Symbol(U, Decl(mappedTypes2.ts, 33, 56)) +>Record : Symbol(Record, Decl(mappedTypes2.ts, 11, 1)) +>K : Symbol(K, Decl(mappedTypes2.ts, 33, 27)) +>U : Symbol(U, Decl(mappedTypes2.ts, 33, 56)) + +declare function proxify(obj: T): Proxify; +>proxify : Symbol(proxify, Decl(mappedTypes2.ts, 33, 109)) +>T : Symbol(T, Decl(mappedTypes2.ts, 34, 25)) +>obj : Symbol(obj, Decl(mappedTypes2.ts, 34, 28)) +>T : Symbol(T, Decl(mappedTypes2.ts, 34, 25)) +>Proxify : Symbol(Proxify, Decl(mappedTypes2.ts, 20, 1)) +>T : Symbol(T, Decl(mappedTypes2.ts, 34, 25)) + +interface Shape { +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + name: string; +>name : Symbol(Shape.name, Decl(mappedTypes2.ts, 36, 17)) + + width: number; +>width : Symbol(Shape.width, Decl(mappedTypes2.ts, 37, 17)) + + height: number; +>height : Symbol(Shape.height, Decl(mappedTypes2.ts, 38, 18)) + + visible: boolean; +>visible : Symbol(Shape.visible, Decl(mappedTypes2.ts, 39, 19)) +} + +interface PartialShape { +>PartialShape : Symbol(PartialShape, Decl(mappedTypes2.ts, 41, 1)) + + name?: string; +>name : Symbol(PartialShape.name, Decl(mappedTypes2.ts, 43, 24)) + + width?: number; +>width : Symbol(PartialShape.width, Decl(mappedTypes2.ts, 44, 18)) + + height?: number; +>height : Symbol(PartialShape.height, Decl(mappedTypes2.ts, 45, 19)) + + visible?: boolean; +>visible : Symbol(PartialShape.visible, Decl(mappedTypes2.ts, 46, 20)) +} + +interface ReadonlyShape { +>ReadonlyShape : Symbol(ReadonlyShape, Decl(mappedTypes2.ts, 48, 1)) + + readonly name: string; +>name : Symbol(ReadonlyShape.name, Decl(mappedTypes2.ts, 50, 25)) + + readonly width: number; +>width : Symbol(ReadonlyShape.width, Decl(mappedTypes2.ts, 51, 26)) + + readonly height: number; +>height : Symbol(ReadonlyShape.height, Decl(mappedTypes2.ts, 52, 27)) + + readonly visible: boolean; +>visible : Symbol(ReadonlyShape.visible, Decl(mappedTypes2.ts, 53, 28)) +} + +function f0(s1: Shape, s2: Shape) { +>f0 : Symbol(f0, Decl(mappedTypes2.ts, 55, 1)) +>s1 : Symbol(s1, Decl(mappedTypes2.ts, 57, 12)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) +>s2 : Symbol(s2, Decl(mappedTypes2.ts, 57, 22)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + assign(s1, { name: "circle" }); +>assign : Symbol(assign, Decl(mappedTypes2.ts, 28, 2)) +>s1 : Symbol(s1, Decl(mappedTypes2.ts, 57, 12)) +>name : Symbol(name, Decl(mappedTypes2.ts, 58, 16)) + + assign(s2, { width: 10, height: 20 }); +>assign : Symbol(assign, Decl(mappedTypes2.ts, 28, 2)) +>s2 : Symbol(s2, Decl(mappedTypes2.ts, 57, 22)) +>width : Symbol(width, Decl(mappedTypes2.ts, 59, 16)) +>height : Symbol(height, Decl(mappedTypes2.ts, 59, 27)) +} + +function f1(shape: Shape) { +>f1 : Symbol(f1, Decl(mappedTypes2.ts, 60, 1)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 62, 12)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + var frozen: ReadonlyShape; +>frozen : Symbol(frozen, Decl(mappedTypes2.ts, 63, 7), Decl(mappedTypes2.ts, 64, 7), Decl(mappedTypes2.ts, 65, 7)) +>ReadonlyShape : Symbol(ReadonlyShape, Decl(mappedTypes2.ts, 48, 1)) + + var frozen: Readonly; +>frozen : Symbol(frozen, Decl(mappedTypes2.ts, 63, 7), Decl(mappedTypes2.ts, 64, 7), Decl(mappedTypes2.ts, 65, 7)) +>Readonly : Symbol(Readonly, Decl(mappedTypes2.ts, 3, 2)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + var frozen = freeze(shape); +>frozen : Symbol(frozen, Decl(mappedTypes2.ts, 63, 7), Decl(mappedTypes2.ts, 64, 7), Decl(mappedTypes2.ts, 65, 7)) +>freeze : Symbol(freeze, Decl(mappedTypes2.ts, 30, 60)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 62, 12)) +} + +function f2(shape: Shape) { +>f2 : Symbol(f2, Decl(mappedTypes2.ts, 66, 1)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 68, 12)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + var partial: PartialShape; +>partial : Symbol(partial, Decl(mappedTypes2.ts, 69, 7), Decl(mappedTypes2.ts, 70, 7), Decl(mappedTypes2.ts, 71, 7)) +>PartialShape : Symbol(PartialShape, Decl(mappedTypes2.ts, 41, 1)) + + var partial: Partial; +>partial : Symbol(partial, Decl(mappedTypes2.ts, 69, 7), Decl(mappedTypes2.ts, 70, 7), Decl(mappedTypes2.ts, 71, 7)) +>Partial : Symbol(Partial, Decl(mappedTypes2.ts, 0, 0)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + var partial: Partial = {}; +>partial : Symbol(partial, Decl(mappedTypes2.ts, 69, 7), Decl(mappedTypes2.ts, 70, 7), Decl(mappedTypes2.ts, 71, 7)) +>Partial : Symbol(Partial, Decl(mappedTypes2.ts, 0, 0)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) +} + +function f3(shape: Shape) { +>f3 : Symbol(f3, Decl(mappedTypes2.ts, 72, 1)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 74, 12)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + const x = pick(shape, "name", "visible"); // { name: string, visible: boolean } +>x : Symbol(x, Decl(mappedTypes2.ts, 75, 9)) +>pick : Symbol(pick, Decl(mappedTypes2.ts, 31, 48)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 74, 12)) +} + +function f4() { +>f4 : Symbol(f4, Decl(mappedTypes2.ts, 76, 1)) + + const rec = { foo: "hello", bar: "world", baz: "bye" }; +>rec : Symbol(rec, Decl(mappedTypes2.ts, 79, 9)) +>foo : Symbol(foo, Decl(mappedTypes2.ts, 79, 17)) +>bar : Symbol(bar, Decl(mappedTypes2.ts, 79, 31)) +>baz : Symbol(baz, Decl(mappedTypes2.ts, 79, 45)) + + const lengths = mapObject(rec, s => s.length); // { foo: number, bar: number, baz: number } +>lengths : Symbol(lengths, Decl(mappedTypes2.ts, 80, 9)) +>mapObject : Symbol(mapObject, Decl(mappedTypes2.ts, 32, 78)) +>rec : Symbol(rec, Decl(mappedTypes2.ts, 79, 9)) +>s : Symbol(s, Decl(mappedTypes2.ts, 80, 34)) +>s.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>s : Symbol(s, Decl(mappedTypes2.ts, 80, 34)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) +} + +function f5(shape: Shape) { +>f5 : Symbol(f5, Decl(mappedTypes2.ts, 81, 1)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 83, 12)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + const p = proxify(shape); +>p : Symbol(p, Decl(mappedTypes2.ts, 84, 9)) +>proxify : Symbol(proxify, Decl(mappedTypes2.ts, 33, 109)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 83, 12)) + + let name = p.name.get(); +>name : Symbol(name, Decl(mappedTypes2.ts, 85, 7)) +>p.name.get : Symbol(get, Decl(mappedTypes2.ts, 17, 17)) +>p.name : Symbol(name) +>p : Symbol(p, Decl(mappedTypes2.ts, 84, 9)) +>name : Symbol(name) +>get : Symbol(get, Decl(mappedTypes2.ts, 17, 17)) + + p.visible.set(false); +>p.visible.set : Symbol(set, Decl(mappedTypes2.ts, 18, 13)) +>p.visible : Symbol(visible) +>p : Symbol(p, Decl(mappedTypes2.ts, 84, 9)) +>visible : Symbol(visible) +>set : Symbol(set, Decl(mappedTypes2.ts, 18, 13)) +} + +function f6(shape: DeepReadonly) { +>f6 : Symbol(f6, Decl(mappedTypes2.ts, 87, 1)) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 89, 12)) +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes2.ts, 24, 1)) +>Shape : Symbol(Shape, Decl(mappedTypes2.ts, 34, 48)) + + let name = shape.name; // DeepReadonly +>name : Symbol(name, Decl(mappedTypes2.ts, 90, 7)) +>shape.name : Symbol(name) +>shape : Symbol(shape, Decl(mappedTypes2.ts, 89, 12)) +>name : Symbol(name) + + let length = name.length; // DeepReadonly +>length : Symbol(length, Decl(mappedTypes2.ts, 91, 7)) +>name.length : Symbol(length) +>name : Symbol(name, Decl(mappedTypes2.ts, 90, 7)) +>length : Symbol(length) + + let toString = length.toString; // DeepReadonly<(radix?: number) => string> +>toString : Symbol(toString, Decl(mappedTypes2.ts, 92, 7)) +>length.toString : Symbol(toString) +>length : Symbol(length, Decl(mappedTypes2.ts, 91, 7)) +>toString : Symbol(toString) +} diff --git a/tests/baselines/reference/mappedTypes2.types b/tests/baselines/reference/mappedTypes2.types new file mode 100644 index 00000000000..e3b67647993 --- /dev/null +++ b/tests/baselines/reference/mappedTypes2.types @@ -0,0 +1,353 @@ +=== tests/cases/conformance/types/mapped/mappedTypes2.ts === + +type Partial = { +>Partial : Partial +>T : T + + [P in keyof T]?: T[P]; +>P : P +>T : T +>T : T +>P : P + +}; + +type Readonly = { +>Readonly : Readonly +>T : T + + readonly [P in keyof T]: T[P]; +>P : P +>T : T +>T : T +>P : P + +}; + +type Pick = { +>Pick : Pick +>T : T +>K : K +>T : T + + [P in K]: T[P]; +>P : P +>K : K +>T : T +>P : P +} + +type Record = { +>Record : Record +>K : K +>T : T + + [_ in K]: T; +>_ : _ +>K : K +>T : T +} + +type Proxy = { +>Proxy : Proxy +>T : T + + get(): T; +>get : () => T +>T : T + + set(value: T): void; +>set : (value: T) => void +>value : T +>T : T +} + +type Proxify = { +>Proxify : Proxify +>T : T + + [P in keyof T]: Proxy; +>P : P +>T : T +>Proxy : Proxy +>T : T +>P : P +} + +type DeepReadonly = { +>DeepReadonly : DeepReadonly +>T : T + + readonly [P in keyof T]: DeepReadonly; +>P : P +>T : T +>DeepReadonly : DeepReadonly +>T : T +>P : P + +}; + +declare function assign(obj: T, props: Partial): void; +>assign : (obj: T, props: Partial) => void +>T : T +>obj : T +>T : T +>props : Partial +>Partial : Partial +>T : T + +declare function freeze(obj: T): Readonly; +>freeze : (obj: T) => Readonly +>T : T +>obj : T +>T : T +>Readonly : Readonly +>T : T + +declare function pick(obj: T, ...keys: K[]): Pick; +>pick : (obj: T, ...keys: K[]) => Pick +>T : T +>K : K +>T : T +>obj : T +>T : T +>keys : K[] +>K : K +>Pick : Pick +>T : T +>K : K + +declare function mapObject(obj: Record, f: (x: T) => U): Record; +>mapObject : (obj: Record, f: (x: T) => U) => Record +>K : K +>T : T +>U : U +>obj : Record +>Record : Record +>K : K +>T : T +>f : (x: T) => U +>x : T +>T : T +>U : U +>Record : Record +>K : K +>U : U + +declare function proxify(obj: T): Proxify; +>proxify : (obj: T) => Proxify +>T : T +>obj : T +>T : T +>Proxify : Proxify +>T : T + +interface Shape { +>Shape : Shape + + name: string; +>name : string + + width: number; +>width : number + + height: number; +>height : number + + visible: boolean; +>visible : boolean +} + +interface PartialShape { +>PartialShape : PartialShape + + name?: string; +>name : string | undefined + + width?: number; +>width : number | undefined + + height?: number; +>height : number | undefined + + visible?: boolean; +>visible : boolean | undefined +} + +interface ReadonlyShape { +>ReadonlyShape : ReadonlyShape + + readonly name: string; +>name : string + + readonly width: number; +>width : number + + readonly height: number; +>height : number + + readonly visible: boolean; +>visible : boolean +} + +function f0(s1: Shape, s2: Shape) { +>f0 : (s1: Shape, s2: Shape) => void +>s1 : Shape +>Shape : Shape +>s2 : Shape +>Shape : Shape + + assign(s1, { name: "circle" }); +>assign(s1, { name: "circle" }) : void +>assign : (obj: T, props: Partial) => void +>s1 : Shape +>{ name: "circle" } : { name: string; } +>name : string +>"circle" : "circle" + + assign(s2, { width: 10, height: 20 }); +>assign(s2, { width: 10, height: 20 }) : void +>assign : (obj: T, props: Partial) => void +>s2 : Shape +>{ width: 10, height: 20 } : { width: number; height: number; } +>width : number +>10 : 10 +>height : number +>20 : 20 +} + +function f1(shape: Shape) { +>f1 : (shape: Shape) => void +>shape : Shape +>Shape : Shape + + var frozen: ReadonlyShape; +>frozen : ReadonlyShape +>ReadonlyShape : ReadonlyShape + + var frozen: Readonly; +>frozen : ReadonlyShape +>Readonly : Readonly +>Shape : Shape + + var frozen = freeze(shape); +>frozen : ReadonlyShape +>freeze(shape) : Readonly +>freeze : (obj: T) => Readonly +>shape : Shape +} + +function f2(shape: Shape) { +>f2 : (shape: Shape) => void +>shape : Shape +>Shape : Shape + + var partial: PartialShape; +>partial : PartialShape +>PartialShape : PartialShape + + var partial: Partial; +>partial : PartialShape +>Partial : Partial +>Shape : Shape + + var partial: Partial = {}; +>partial : PartialShape +>Partial : Partial +>Shape : Shape +>{} : {} +} + +function f3(shape: Shape) { +>f3 : (shape: Shape) => void +>shape : Shape +>Shape : Shape + + const x = pick(shape, "name", "visible"); // { name: string, visible: boolean } +>x : Pick +>pick(shape, "name", "visible") : Pick +>pick : (obj: T, ...keys: K[]) => Pick +>shape : Shape +>"name" : "name" +>"visible" : "visible" +} + +function f4() { +>f4 : () => void + + const rec = { foo: "hello", bar: "world", baz: "bye" }; +>rec : { foo: string; bar: string; baz: string; } +>{ foo: "hello", bar: "world", baz: "bye" } : { foo: string; bar: string; baz: string; } +>foo : string +>"hello" : "hello" +>bar : string +>"world" : "world" +>baz : string +>"bye" : "bye" + + const lengths = mapObject(rec, s => s.length); // { foo: number, bar: number, baz: number } +>lengths : Record<"foo" | "bar" | "baz", number> +>mapObject(rec, s => s.length) : Record<"foo" | "bar" | "baz", number> +>mapObject : (obj: Record, f: (x: T) => U) => Record +>rec : { foo: string; bar: string; baz: string; } +>s => s.length : (s: string) => number +>s : string +>s.length : number +>s : string +>length : number +} + +function f5(shape: Shape) { +>f5 : (shape: Shape) => void +>shape : Shape +>Shape : Shape + + const p = proxify(shape); +>p : Proxify +>proxify(shape) : Proxify +>proxify : (obj: T) => Proxify +>shape : Shape + + let name = p.name.get(); +>name : string +>p.name.get() : string +>p.name.get : () => string +>p.name : Proxy +>p : Proxify +>name : Proxy +>get : () => string + + p.visible.set(false); +>p.visible.set(false) : void +>p.visible.set : (value: boolean) => void +>p.visible : Proxy +>p : Proxify +>visible : Proxy +>set : (value: boolean) => void +>false : false +} + +function f6(shape: DeepReadonly) { +>f6 : (shape: DeepReadonly) => void +>shape : DeepReadonly +>DeepReadonly : DeepReadonly +>Shape : Shape + + let name = shape.name; // DeepReadonly +>name : DeepReadonly +>shape.name : DeepReadonly +>shape : DeepReadonly +>name : DeepReadonly + + let length = name.length; // DeepReadonly +>length : DeepReadonly +>name.length : DeepReadonly +>name : DeepReadonly +>length : DeepReadonly + + let toString = length.toString; // DeepReadonly<(radix?: number) => string> +>toString : DeepReadonly<(radix?: number | undefined) => string> +>length.toString : DeepReadonly<(radix?: number | undefined) => string> +>length : DeepReadonly +>toString : DeepReadonly<(radix?: number | undefined) => string> +} diff --git a/tests/baselines/reference/mappedTypes3.js b/tests/baselines/reference/mappedTypes3.js new file mode 100644 index 00000000000..712822e8728 --- /dev/null +++ b/tests/baselines/reference/mappedTypes3.js @@ -0,0 +1,84 @@ +//// [mappedTypes3.ts] + +class Box

{ + value: P; +} + +type Boxified = { + [K in keyof T]: Box; +} + +declare function boxify(obj: T): Boxified; +declare function unboxify(obj: Boxified): T; + +interface Bacon { + isPerfect: boolean; + weight: number; +} + +interface BoxifiedBacon { + isPerfect: Box; + weight: Box; +} + +function f1(b: Bacon) { + let bb = boxify(b); + let isPerfect = bb.isPerfect.value; + let weight = bb.weight.value; +} + +function f2(bb: Boxified) { + let b = unboxify(bb); // Infer Bacon for T + let bool = b.isPerfect; + let weight = b.weight; +} + +function f3(bb: BoxifiedBacon) { + let b = unboxify(bb); // Explicit type parameter required + let bool = b.isPerfect; + let weight = bb.weight; +} + +//// [mappedTypes3.js] +var Box = (function () { + function Box() { + } + return Box; +}()); +function f1(b) { + var bb = boxify(b); + var isPerfect = bb.isPerfect.value; + var weight = bb.weight.value; +} +function f2(bb) { + var b = unboxify(bb); // Infer Bacon for T + var bool = b.isPerfect; + var weight = b.weight; +} +function f3(bb) { + var b = unboxify(bb); // Explicit type parameter required + var bool = b.isPerfect; + var weight = bb.weight; +} + + +//// [mappedTypes3.d.ts] +declare class Box

{ + value: P; +} +declare type Boxified = { + [K in keyof T]: Box; +}; +declare function boxify(obj: T): Boxified; +declare function unboxify(obj: Boxified): T; +interface Bacon { + isPerfect: boolean; + weight: number; +} +interface BoxifiedBacon { + isPerfect: Box; + weight: Box; +} +declare function f1(b: Bacon): void; +declare function f2(bb: Boxified): void; +declare function f3(bb: BoxifiedBacon): void; diff --git a/tests/baselines/reference/mappedTypes3.symbols b/tests/baselines/reference/mappedTypes3.symbols new file mode 100644 index 00000000000..3d909c8e532 --- /dev/null +++ b/tests/baselines/reference/mappedTypes3.symbols @@ -0,0 +1,135 @@ +=== tests/cases/conformance/types/mapped/mappedTypes3.ts === + +class Box

{ +>Box : Symbol(Box, Decl(mappedTypes3.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypes3.ts, 1, 10)) + + value: P; +>value : Symbol(Box.value, Decl(mappedTypes3.ts, 1, 14)) +>P : Symbol(P, Decl(mappedTypes3.ts, 1, 10)) +} + +type Boxified = { +>Boxified : Symbol(Boxified, Decl(mappedTypes3.ts, 3, 1)) +>T : Symbol(T, Decl(mappedTypes3.ts, 5, 14)) + + [K in keyof T]: Box; +>K : Symbol(K, Decl(mappedTypes3.ts, 6, 5)) +>T : Symbol(T, Decl(mappedTypes3.ts, 5, 14)) +>Box : Symbol(Box, Decl(mappedTypes3.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes3.ts, 5, 14)) +>K : Symbol(K, Decl(mappedTypes3.ts, 6, 5)) +} + +declare function boxify(obj: T): Boxified; +>boxify : Symbol(boxify, Decl(mappedTypes3.ts, 7, 1)) +>T : Symbol(T, Decl(mappedTypes3.ts, 9, 24)) +>obj : Symbol(obj, Decl(mappedTypes3.ts, 9, 27)) +>T : Symbol(T, Decl(mappedTypes3.ts, 9, 24)) +>Boxified : Symbol(Boxified, Decl(mappedTypes3.ts, 3, 1)) +>T : Symbol(T, Decl(mappedTypes3.ts, 9, 24)) + +declare function unboxify(obj: Boxified): T; +>unboxify : Symbol(unboxify, Decl(mappedTypes3.ts, 9, 48)) +>T : Symbol(T, Decl(mappedTypes3.ts, 10, 26)) +>obj : Symbol(obj, Decl(mappedTypes3.ts, 10, 29)) +>Boxified : Symbol(Boxified, Decl(mappedTypes3.ts, 3, 1)) +>T : Symbol(T, Decl(mappedTypes3.ts, 10, 26)) +>T : Symbol(T, Decl(mappedTypes3.ts, 10, 26)) + +interface Bacon { +>Bacon : Symbol(Bacon, Decl(mappedTypes3.ts, 10, 50)) + + isPerfect: boolean; +>isPerfect : Symbol(Bacon.isPerfect, Decl(mappedTypes3.ts, 12, 17)) + + weight: number; +>weight : Symbol(Bacon.weight, Decl(mappedTypes3.ts, 13, 23)) +} + +interface BoxifiedBacon { +>BoxifiedBacon : Symbol(BoxifiedBacon, Decl(mappedTypes3.ts, 15, 1)) + + isPerfect: Box; +>isPerfect : Symbol(BoxifiedBacon.isPerfect, Decl(mappedTypes3.ts, 17, 25)) +>Box : Symbol(Box, Decl(mappedTypes3.ts, 0, 0)) + + weight: Box; +>weight : Symbol(BoxifiedBacon.weight, Decl(mappedTypes3.ts, 18, 28)) +>Box : Symbol(Box, Decl(mappedTypes3.ts, 0, 0)) +} + +function f1(b: Bacon) { +>f1 : Symbol(f1, Decl(mappedTypes3.ts, 20, 1)) +>b : Symbol(b, Decl(mappedTypes3.ts, 22, 12)) +>Bacon : Symbol(Bacon, Decl(mappedTypes3.ts, 10, 50)) + + let bb = boxify(b); +>bb : Symbol(bb, Decl(mappedTypes3.ts, 23, 7)) +>boxify : Symbol(boxify, Decl(mappedTypes3.ts, 7, 1)) +>b : Symbol(b, Decl(mappedTypes3.ts, 22, 12)) + + let isPerfect = bb.isPerfect.value; +>isPerfect : Symbol(isPerfect, Decl(mappedTypes3.ts, 24, 7)) +>bb.isPerfect.value : Symbol(Box.value, Decl(mappedTypes3.ts, 1, 14)) +>bb.isPerfect : Symbol(isPerfect) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 23, 7)) +>isPerfect : Symbol(isPerfect) +>value : Symbol(Box.value, Decl(mappedTypes3.ts, 1, 14)) + + let weight = bb.weight.value; +>weight : Symbol(weight, Decl(mappedTypes3.ts, 25, 7)) +>bb.weight.value : Symbol(Box.value, Decl(mappedTypes3.ts, 1, 14)) +>bb.weight : Symbol(weight) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 23, 7)) +>weight : Symbol(weight) +>value : Symbol(Box.value, Decl(mappedTypes3.ts, 1, 14)) +} + +function f2(bb: Boxified) { +>f2 : Symbol(f2, Decl(mappedTypes3.ts, 26, 1)) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 28, 12)) +>Boxified : Symbol(Boxified, Decl(mappedTypes3.ts, 3, 1)) +>Bacon : Symbol(Bacon, Decl(mappedTypes3.ts, 10, 50)) + + let b = unboxify(bb); // Infer Bacon for T +>b : Symbol(b, Decl(mappedTypes3.ts, 29, 7)) +>unboxify : Symbol(unboxify, Decl(mappedTypes3.ts, 9, 48)) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 28, 12)) + + let bool = b.isPerfect; +>bool : Symbol(bool, Decl(mappedTypes3.ts, 30, 7)) +>b.isPerfect : Symbol(Bacon.isPerfect, Decl(mappedTypes3.ts, 12, 17)) +>b : Symbol(b, Decl(mappedTypes3.ts, 29, 7)) +>isPerfect : Symbol(Bacon.isPerfect, Decl(mappedTypes3.ts, 12, 17)) + + let weight = b.weight; +>weight : Symbol(weight, Decl(mappedTypes3.ts, 31, 7)) +>b.weight : Symbol(Bacon.weight, Decl(mappedTypes3.ts, 13, 23)) +>b : Symbol(b, Decl(mappedTypes3.ts, 29, 7)) +>weight : Symbol(Bacon.weight, Decl(mappedTypes3.ts, 13, 23)) +} + +function f3(bb: BoxifiedBacon) { +>f3 : Symbol(f3, Decl(mappedTypes3.ts, 32, 1)) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 34, 12)) +>BoxifiedBacon : Symbol(BoxifiedBacon, Decl(mappedTypes3.ts, 15, 1)) + + let b = unboxify(bb); // Explicit type parameter required +>b : Symbol(b, Decl(mappedTypes3.ts, 35, 7)) +>unboxify : Symbol(unboxify, Decl(mappedTypes3.ts, 9, 48)) +>Bacon : Symbol(Bacon, Decl(mappedTypes3.ts, 10, 50)) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 34, 12)) + + let bool = b.isPerfect; +>bool : Symbol(bool, Decl(mappedTypes3.ts, 36, 7)) +>b.isPerfect : Symbol(Bacon.isPerfect, Decl(mappedTypes3.ts, 12, 17)) +>b : Symbol(b, Decl(mappedTypes3.ts, 35, 7)) +>isPerfect : Symbol(Bacon.isPerfect, Decl(mappedTypes3.ts, 12, 17)) + + let weight = bb.weight; +>weight : Symbol(weight, Decl(mappedTypes3.ts, 37, 7)) +>bb.weight : Symbol(BoxifiedBacon.weight, Decl(mappedTypes3.ts, 18, 28)) +>bb : Symbol(bb, Decl(mappedTypes3.ts, 34, 12)) +>weight : Symbol(BoxifiedBacon.weight, Decl(mappedTypes3.ts, 18, 28)) +} diff --git a/tests/baselines/reference/mappedTypes3.types b/tests/baselines/reference/mappedTypes3.types new file mode 100644 index 00000000000..36471938d75 --- /dev/null +++ b/tests/baselines/reference/mappedTypes3.types @@ -0,0 +1,138 @@ +=== tests/cases/conformance/types/mapped/mappedTypes3.ts === + +class Box

{ +>Box : Box

+>P : P + + value: P; +>value : P +>P : P +} + +type Boxified = { +>Boxified : Boxified +>T : T + + [K in keyof T]: Box; +>K : K +>T : T +>Box : Box

+>T : T +>K : K +} + +declare function boxify(obj: T): Boxified; +>boxify : (obj: T) => Boxified +>T : T +>obj : T +>T : T +>Boxified : Boxified +>T : T + +declare function unboxify(obj: Boxified): T; +>unboxify : (obj: Boxified) => T +>T : T +>obj : Boxified +>Boxified : Boxified +>T : T +>T : T + +interface Bacon { +>Bacon : Bacon + + isPerfect: boolean; +>isPerfect : boolean + + weight: number; +>weight : number +} + +interface BoxifiedBacon { +>BoxifiedBacon : BoxifiedBacon + + isPerfect: Box; +>isPerfect : Box +>Box : Box

+ + weight: Box; +>weight : Box +>Box : Box

+} + +function f1(b: Bacon) { +>f1 : (b: Bacon) => void +>b : Bacon +>Bacon : Bacon + + let bb = boxify(b); +>bb : Boxified +>boxify(b) : Boxified +>boxify : (obj: T) => Boxified +>b : Bacon + + let isPerfect = bb.isPerfect.value; +>isPerfect : boolean +>bb.isPerfect.value : boolean +>bb.isPerfect : Box +>bb : Boxified +>isPerfect : Box +>value : boolean + + let weight = bb.weight.value; +>weight : number +>bb.weight.value : number +>bb.weight : Box +>bb : Boxified +>weight : Box +>value : number +} + +function f2(bb: Boxified) { +>f2 : (bb: Boxified) => void +>bb : Boxified +>Boxified : Boxified +>Bacon : Bacon + + let b = unboxify(bb); // Infer Bacon for T +>b : Bacon +>unboxify(bb) : Bacon +>unboxify : (obj: Boxified) => T +>bb : Boxified + + let bool = b.isPerfect; +>bool : boolean +>b.isPerfect : boolean +>b : Bacon +>isPerfect : boolean + + let weight = b.weight; +>weight : number +>b.weight : number +>b : Bacon +>weight : number +} + +function f3(bb: BoxifiedBacon) { +>f3 : (bb: BoxifiedBacon) => void +>bb : BoxifiedBacon +>BoxifiedBacon : BoxifiedBacon + + let b = unboxify(bb); // Explicit type parameter required +>b : Bacon +>unboxify(bb) : Bacon +>unboxify : (obj: Boxified) => T +>Bacon : Bacon +>bb : BoxifiedBacon + + let bool = b.isPerfect; +>bool : boolean +>b.isPerfect : boolean +>b : Bacon +>isPerfect : boolean + + let weight = bb.weight; +>weight : Box +>bb.weight : Box +>bb : BoxifiedBacon +>weight : Box +} diff --git a/tests/cases/conformance/types/mapped/mappedTypes1.ts b/tests/cases/conformance/types/mapped/mappedTypes1.ts new file mode 100644 index 00000000000..f57a14a2e10 --- /dev/null +++ b/tests/cases/conformance/types/mapped/mappedTypes1.ts @@ -0,0 +1,35 @@ +// @strictNullChecks: true +// @declaration: true + +type Item = { a: string, b: number, c: boolean }; + +type T00 = { [P in "x" | "y"]: number }; +type T01 = { [P in "x" | "y"]: P }; +type T02 = { [P in "a" | "b"]: Item[P]; } +type T03 = { [P in keyof Item]: Date }; + +type T10 = { [P in keyof Item]: Item[P] }; +type T11 = { [P in keyof Item]?: Item[P] }; +type T12 = { readonly [P in keyof Item]: Item[P] }; +type T13 = { readonly [P in keyof Item]?: Item[P] }; + +type T20 = { [P in keyof Item]: Item[P] | null }; +type T21 = { [P in keyof Item]: Array }; + +type T30 = { [P in keyof any]: void }; +type T31 = { [P in keyof string]: void }; +type T32 = { [P in keyof number]: void }; +type T33 = { [P in keyof boolean]: void }; +type T34 = { [P in keyof undefined]: void }; +type T35 = { [P in keyof null]: void }; +type T36 = { [P in keyof void]: void }; +type T37 = { [P in keyof symbol]: void }; +type T38 = { [P in keyof never]: void }; + +declare function f1(): { [P in keyof T1]: void }; +declare function f2(): { [P in keyof T1]: void }; +declare function f3(): { [P in keyof T1]: void }; + +let x1 = f1(); +let x2 = f2(); +let x3 = f3(); \ No newline at end of file diff --git a/tests/cases/conformance/types/mapped/mappedTypes2.ts b/tests/cases/conformance/types/mapped/mappedTypes2.ts new file mode 100644 index 00000000000..84bffe2ea17 --- /dev/null +++ b/tests/cases/conformance/types/mapped/mappedTypes2.ts @@ -0,0 +1,96 @@ +// @strictNullChecks: true +// @declaration: true + +type Partial = { + [P in keyof T]?: T[P]; +}; + +type Readonly = { + readonly [P in keyof T]: T[P]; +}; + +type Pick = { + [P in K]: T[P]; +} + +type Record = { + [_ in K]: T; +} + +type Proxy = { + get(): T; + set(value: T): void; +} + +type Proxify = { + [P in keyof T]: Proxy; +} + +type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; + +declare function assign(obj: T, props: Partial): void; +declare function freeze(obj: T): Readonly; +declare function pick(obj: T, ...keys: K[]): Pick; +declare function mapObject(obj: Record, f: (x: T) => U): Record; +declare function proxify(obj: T): Proxify; + +interface Shape { + name: string; + width: number; + height: number; + visible: boolean; +} + +interface PartialShape { + name?: string; + width?: number; + height?: number; + visible?: boolean; +} + +interface ReadonlyShape { + readonly name: string; + readonly width: number; + readonly height: number; + readonly visible: boolean; +} + +function f0(s1: Shape, s2: Shape) { + assign(s1, { name: "circle" }); + assign(s2, { width: 10, height: 20 }); +} + +function f1(shape: Shape) { + var frozen: ReadonlyShape; + var frozen: Readonly; + var frozen = freeze(shape); +} + +function f2(shape: Shape) { + var partial: PartialShape; + var partial: Partial; + var partial: Partial = {}; +} + +function f3(shape: Shape) { + const x = pick(shape, "name", "visible"); // { name: string, visible: boolean } +} + +function f4() { + const rec = { foo: "hello", bar: "world", baz: "bye" }; + const lengths = mapObject(rec, s => s.length); // { foo: number, bar: number, baz: number } +} + +function f5(shape: Shape) { + const p = proxify(shape); + let name = p.name.get(); + p.visible.set(false); +} + +function f6(shape: DeepReadonly) { + let name = shape.name; // DeepReadonly + let length = name.length; // DeepReadonly + let toString = length.toString; // DeepReadonly<(radix?: number) => string> +} \ No newline at end of file diff --git a/tests/cases/conformance/types/mapped/mappedTypes3.ts b/tests/cases/conformance/types/mapped/mappedTypes3.ts new file mode 100644 index 00000000000..bf5a4399b0d --- /dev/null +++ b/tests/cases/conformance/types/mapped/mappedTypes3.ts @@ -0,0 +1,40 @@ +// @declaration: true + +class Box

{ + value: P; +} + +type Boxified = { + [K in keyof T]: Box; +} + +declare function boxify(obj: T): Boxified; +declare function unboxify(obj: Boxified): T; + +interface Bacon { + isPerfect: boolean; + weight: number; +} + +interface BoxifiedBacon { + isPerfect: Box; + weight: Box; +} + +function f1(b: Bacon) { + let bb = boxify(b); + let isPerfect = bb.isPerfect.value; + let weight = bb.weight.value; +} + +function f2(bb: Boxified) { + let b = unboxify(bb); // Infer Bacon for T + let bool = b.isPerfect; + let weight = b.weight; +} + +function f3(bb: BoxifiedBacon) { + let b = unboxify(bb); // Explicit type parameter required + let bool = b.isPerfect; + let weight = bb.weight; +} \ No newline at end of file