From 43e383f20b996aafe644a665b15f39386d6bc64a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 22 Nov 2016 14:28:45 -0800 Subject: [PATCH] Add more tests --- tests/baselines/reference/mappedTypes4.js | 129 +++++++++++ .../baselines/reference/mappedTypes4.symbols | 198 ++++++++++++++++ tests/baselines/reference/mappedTypes4.types | 213 ++++++++++++++++++ .../conformance/types/mapped/mappedTypes4.ts | 62 +++++ 4 files changed, 602 insertions(+) create mode 100644 tests/baselines/reference/mappedTypes4.js create mode 100644 tests/baselines/reference/mappedTypes4.symbols create mode 100644 tests/baselines/reference/mappedTypes4.types create mode 100644 tests/cases/conformance/types/mapped/mappedTypes4.ts diff --git a/tests/baselines/reference/mappedTypes4.js b/tests/baselines/reference/mappedTypes4.js new file mode 100644 index 00000000000..bb003b1a1ec --- /dev/null +++ b/tests/baselines/reference/mappedTypes4.js @@ -0,0 +1,129 @@ +//// [mappedTypes4.ts] + +type Box = { +}; + +type Boxified = { + [P in keyof T]: Box; +}; + +function boxify(obj: T): Boxified { + if (typeof obj === "object") { + let result = {} as Boxified; + for (let k in obj) { + result[k] = { value: obj[k] }; + } + return result; + } + return obj; +} + +type A = { a: string }; +type B = { b: string }; +type C = { c: string }; + +function f1(x: A | B | C | undefined) { + return boxify(x); +} + +type T00 = Partial; +type T01 = Readonly; +type T02 = Boxified +type T03 = Readonly; +type T04 = Boxified; +type T05 = Partial<"hello" | "world" | 42>; + +type BoxifiedWithSentinel = { + [P in keyof T]: Box | U; +} + +type T10 = BoxifiedWithSentinel; +type T11 = BoxifiedWithSentinel; +type T12 = BoxifiedWithSentinel; + +type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; + +type Foo = { + x: number; + y: { a: string, b: number }; + z: boolean; +}; + +type DeepReadonlyFoo = { + readonly x: number; + readonly y: { readonly a: string, readonly b: number }; + readonly z: boolean; +}; + +var x1: DeepReadonly; +var x1: DeepReadonlyFoo; + +//// [mappedTypes4.js] +function boxify(obj) { + if (typeof obj === "object") { + var result = {}; + for (var k in obj) { + result[k] = { value: obj[k] }; + } + return result; + } + return obj; +} +function f1(x) { + return boxify(x); +} +var x1; +var x1; + + +//// [mappedTypes4.d.ts] +declare type Box = {}; +declare type Boxified = { + [P in keyof T]: Box; +}; +declare function boxify(obj: T): Boxified; +declare type A = { + a: string; +}; +declare type B = { + b: string; +}; +declare type C = { + c: string; +}; +declare function f1(x: A | B | C | undefined): Boxified | Boxified | Boxified | undefined; +declare type T00 = Partial; +declare type T01 = Readonly; +declare type T02 = Boxified; +declare type T03 = Readonly; +declare type T04 = Boxified; +declare type T05 = Partial<"hello" | "world" | 42>; +declare type BoxifiedWithSentinel = { + [P in keyof T]: Box | U; +}; +declare type T10 = BoxifiedWithSentinel; +declare type T11 = BoxifiedWithSentinel; +declare type T12 = BoxifiedWithSentinel; +declare type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; +declare type Foo = { + x: number; + y: { + a: string; + b: number; + }; + z: boolean; +}; +declare type DeepReadonlyFoo = { + readonly x: number; + readonly y: { + readonly a: string; + readonly b: number; + }; + readonly z: boolean; +}; +declare var x1: DeepReadonly; +declare var x1: DeepReadonlyFoo; diff --git a/tests/baselines/reference/mappedTypes4.symbols b/tests/baselines/reference/mappedTypes4.symbols new file mode 100644 index 00000000000..b57adfe9954 --- /dev/null +++ b/tests/baselines/reference/mappedTypes4.symbols @@ -0,0 +1,198 @@ +=== tests/cases/conformance/types/mapped/mappedTypes4.ts === + +type Box = { +>Box : Symbol(Box, Decl(mappedTypes4.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes4.ts, 1, 9)) + +}; + +type Boxified = { +>Boxified : Symbol(Boxified, Decl(mappedTypes4.ts, 2, 2)) +>T : Symbol(T, Decl(mappedTypes4.ts, 4, 14)) + + [P in keyof T]: Box; +>P : Symbol(P, Decl(mappedTypes4.ts, 5, 5)) +>T : Symbol(T, Decl(mappedTypes4.ts, 4, 14)) +>Box : Symbol(Box, Decl(mappedTypes4.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes4.ts, 4, 14)) +>P : Symbol(P, Decl(mappedTypes4.ts, 5, 5)) + +}; + +function boxify(obj: T): Boxified { +>boxify : Symbol(boxify, Decl(mappedTypes4.ts, 6, 2)) +>T : Symbol(T, Decl(mappedTypes4.ts, 8, 16)) +>obj : Symbol(obj, Decl(mappedTypes4.ts, 8, 19)) +>T : Symbol(T, Decl(mappedTypes4.ts, 8, 16)) +>Boxified : Symbol(Boxified, Decl(mappedTypes4.ts, 2, 2)) +>T : Symbol(T, Decl(mappedTypes4.ts, 8, 16)) + + if (typeof obj === "object") { +>obj : Symbol(obj, Decl(mappedTypes4.ts, 8, 19)) + + let result = {} as Boxified; +>result : Symbol(result, Decl(mappedTypes4.ts, 10, 11)) +>Boxified : Symbol(Boxified, Decl(mappedTypes4.ts, 2, 2)) +>T : Symbol(T, Decl(mappedTypes4.ts, 8, 16)) + + for (let k in obj) { +>k : Symbol(k, Decl(mappedTypes4.ts, 11, 16)) +>obj : Symbol(obj, Decl(mappedTypes4.ts, 8, 19)) + + result[k] = { value: obj[k] }; +>result : Symbol(result, Decl(mappedTypes4.ts, 10, 11)) +>k : Symbol(k, Decl(mappedTypes4.ts, 11, 16)) +>value : Symbol(value, Decl(mappedTypes4.ts, 12, 25)) +>obj : Symbol(obj, Decl(mappedTypes4.ts, 8, 19)) +>k : Symbol(k, Decl(mappedTypes4.ts, 11, 16)) + } + return result; +>result : Symbol(result, Decl(mappedTypes4.ts, 10, 11)) + } + return obj; +>obj : Symbol(obj, Decl(mappedTypes4.ts, 8, 19)) +} + +type A = { a: string }; +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>a : Symbol(a, Decl(mappedTypes4.ts, 19, 10)) + +type B = { b: string }; +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>b : Symbol(b, Decl(mappedTypes4.ts, 20, 10)) + +type C = { c: string }; +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) +>c : Symbol(c, Decl(mappedTypes4.ts, 21, 10)) + +function f1(x: A | B | C | undefined) { +>f1 : Symbol(f1, Decl(mappedTypes4.ts, 21, 23)) +>x : Symbol(x, Decl(mappedTypes4.ts, 23, 12)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + + return boxify(x); +>boxify : Symbol(boxify, Decl(mappedTypes4.ts, 6, 2)) +>x : Symbol(x, Decl(mappedTypes4.ts, 23, 12)) +} + +type T00 = Partial; +>T00 : Symbol(T00, Decl(mappedTypes4.ts, 25, 1)) +>Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + +type T01 = Readonly; +>T01 : Symbol(T01, Decl(mappedTypes4.ts, 27, 30)) +>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + +type T02 = Boxified +>T02 : Symbol(T02, Decl(mappedTypes4.ts, 28, 50)) +>Boxified : Symbol(Boxified, Decl(mappedTypes4.ts, 2, 2)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + +type T03 = Readonly; +>T03 : Symbol(T03, Decl(mappedTypes4.ts, 29, 41)) +>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) + +type T04 = Boxified; +>T04 : Symbol(T04, Decl(mappedTypes4.ts, 30, 73)) +>Boxified : Symbol(Boxified, Decl(mappedTypes4.ts, 2, 2)) + +type T05 = Partial<"hello" | "world" | 42>; +>T05 : Symbol(T05, Decl(mappedTypes4.ts, 31, 73)) +>Partial : Symbol(Partial, Decl(lib.d.ts, --, --)) + +type BoxifiedWithSentinel = { +>BoxifiedWithSentinel : Symbol(BoxifiedWithSentinel, Decl(mappedTypes4.ts, 32, 43)) +>T : Symbol(T, Decl(mappedTypes4.ts, 34, 26)) +>U : Symbol(U, Decl(mappedTypes4.ts, 34, 28)) + + [P in keyof T]: Box | U; +>P : Symbol(P, Decl(mappedTypes4.ts, 35, 5)) +>T : Symbol(T, Decl(mappedTypes4.ts, 34, 26)) +>Box : Symbol(Box, Decl(mappedTypes4.ts, 0, 0)) +>T : Symbol(T, Decl(mappedTypes4.ts, 34, 26)) +>P : Symbol(P, Decl(mappedTypes4.ts, 35, 5)) +>U : Symbol(U, Decl(mappedTypes4.ts, 34, 28)) +} + +type T10 = BoxifiedWithSentinel; +>T10 : Symbol(T10, Decl(mappedTypes4.ts, 36, 1)) +>BoxifiedWithSentinel : Symbol(BoxifiedWithSentinel, Decl(mappedTypes4.ts, 32, 43)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + +type T11 = BoxifiedWithSentinel; +>T11 : Symbol(T11, Decl(mappedTypes4.ts, 38, 49)) +>BoxifiedWithSentinel : Symbol(BoxifiedWithSentinel, Decl(mappedTypes4.ts, 32, 43)) +>A : Symbol(A, Decl(mappedTypes4.ts, 17, 1)) +>B : Symbol(B, Decl(mappedTypes4.ts, 19, 23)) +>C : Symbol(C, Decl(mappedTypes4.ts, 20, 23)) + +type T12 = BoxifiedWithSentinel; +>T12 : Symbol(T12, Decl(mappedTypes4.ts, 39, 54)) +>BoxifiedWithSentinel : Symbol(BoxifiedWithSentinel, Decl(mappedTypes4.ts, 32, 43)) + +type DeepReadonly = { +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes4.ts, 40, 51)) +>T : Symbol(T, Decl(mappedTypes4.ts, 42, 18)) + + readonly [P in keyof T]: DeepReadonly; +>P : Symbol(P, Decl(mappedTypes4.ts, 43, 14)) +>T : Symbol(T, Decl(mappedTypes4.ts, 42, 18)) +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes4.ts, 40, 51)) +>T : Symbol(T, Decl(mappedTypes4.ts, 42, 18)) +>P : Symbol(P, Decl(mappedTypes4.ts, 43, 14)) + +}; + +type Foo = { +>Foo : Symbol(Foo, Decl(mappedTypes4.ts, 44, 2)) + + x: number; +>x : Symbol(x, Decl(mappedTypes4.ts, 46, 12)) + + y: { a: string, b: number }; +>y : Symbol(y, Decl(mappedTypes4.ts, 47, 14)) +>a : Symbol(a, Decl(mappedTypes4.ts, 48, 8)) +>b : Symbol(b, Decl(mappedTypes4.ts, 48, 19)) + + z: boolean; +>z : Symbol(z, Decl(mappedTypes4.ts, 48, 32)) + +}; + +type DeepReadonlyFoo = { +>DeepReadonlyFoo : Symbol(DeepReadonlyFoo, Decl(mappedTypes4.ts, 50, 2)) + + readonly x: number; +>x : Symbol(x, Decl(mappedTypes4.ts, 52, 24)) + + readonly y: { readonly a: string, readonly b: number }; +>y : Symbol(y, Decl(mappedTypes4.ts, 53, 23)) +>a : Symbol(a, Decl(mappedTypes4.ts, 54, 17)) +>b : Symbol(b, Decl(mappedTypes4.ts, 54, 37)) + + readonly z: boolean; +>z : Symbol(z, Decl(mappedTypes4.ts, 54, 59)) + +}; + +var x1: DeepReadonly; +>x1 : Symbol(x1, Decl(mappedTypes4.ts, 58, 3), Decl(mappedTypes4.ts, 59, 3)) +>DeepReadonly : Symbol(DeepReadonly, Decl(mappedTypes4.ts, 40, 51)) +>Foo : Symbol(Foo, Decl(mappedTypes4.ts, 44, 2)) + +var x1: DeepReadonlyFoo; +>x1 : Symbol(x1, Decl(mappedTypes4.ts, 58, 3), Decl(mappedTypes4.ts, 59, 3)) +>DeepReadonlyFoo : Symbol(DeepReadonlyFoo, Decl(mappedTypes4.ts, 50, 2)) + diff --git a/tests/baselines/reference/mappedTypes4.types b/tests/baselines/reference/mappedTypes4.types new file mode 100644 index 00000000000..cc492d82120 --- /dev/null +++ b/tests/baselines/reference/mappedTypes4.types @@ -0,0 +1,213 @@ +=== tests/cases/conformance/types/mapped/mappedTypes4.ts === + +type Box = { +>Box : Box +>T : T + +}; + +type Boxified = { +>Boxified : Boxified +>T : T + + [P in keyof T]: Box; +>P : P +>T : T +>Box : Box +>T : T +>P : P + +}; + +function boxify(obj: T): Boxified { +>boxify : (obj: T) => Boxified +>T : T +>obj : T +>T : T +>Boxified : Boxified +>T : T + + if (typeof obj === "object") { +>typeof obj === "object" : boolean +>typeof obj : string +>obj : T +>"object" : "object" + + 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] = { value: obj[k] }; +>result[k] = { value: obj[k] } : { value: T[keyof T]; } +>result[k] : Box +>result : Boxified +>k : keyof T +>{ value: obj[k] } : { value: T[keyof T]; } +>value : T[keyof T] +>obj[k] : T[keyof T] +>obj : T +>k : keyof T + } + return result; +>result : Boxified + } + return obj; +>obj : any +>obj : never +} + +type A = { a: string }; +>A : A +>a : string + +type B = { b: string }; +>B : B +>b : string + +type C = { c: string }; +>C : C +>c : string + +function f1(x: A | B | C | undefined) { +>f1 : (x: A | B | C | undefined) => Boxified | Boxified | Boxified | undefined +>x : A | B | C | undefined +>A : A +>B : B +>C : C + + return boxify(x); +>boxify(x) : Boxified | Boxified | Boxified | undefined +>boxify : (obj: T) => Boxified +>x : A | B | C | undefined +} + +type T00 = Partial; +>T00 : Partial | Partial | Partial +>Partial : Partial +>A : A +>B : B +>C : C + +type T01 = Readonly; +>T01 : Readonly | Readonly | Readonly | null | undefined +>Readonly : Readonly +>A : A +>B : B +>C : C +>null : null + +type T02 = Boxified +>T02 : string | Boxified | Boxified | Boxified +>Boxified : Boxified +>A : A +>B : B +>C : C + +type T03 = Readonly; +>T03 : string | number | boolean | void | null | undefined +>Readonly : Readonly +>null : null + +type T04 = Boxified; +>T04 : string | number | boolean | void | null | undefined +>Boxified : Boxified +>null : null + +type T05 = Partial<"hello" | "world" | 42>; +>T05 : "hello" | "world" | 42 +>Partial : Partial + +type BoxifiedWithSentinel = { +>BoxifiedWithSentinel : BoxifiedWithSentinel +>T : T +>U : U + + [P in keyof T]: Box | U; +>P : P +>T : T +>Box : Box +>T : T +>P : P +>U : U +} + +type T10 = BoxifiedWithSentinel; +>T10 : BoxifiedWithSentinel | BoxifiedWithSentinel | BoxifiedWithSentinel +>BoxifiedWithSentinel : BoxifiedWithSentinel +>A : A +>B : B +>C : C +>null : null + +type T11 = BoxifiedWithSentinel; +>T11 : BoxifiedWithSentinel | BoxifiedWithSentinel | BoxifiedWithSentinel +>BoxifiedWithSentinel : BoxifiedWithSentinel +>A : A +>B : B +>C : C + +type T12 = BoxifiedWithSentinel; +>T12 : string +>BoxifiedWithSentinel : BoxifiedWithSentinel + +type DeepReadonly = { +>DeepReadonly : DeepReadonly +>T : T + + readonly [P in keyof T]: DeepReadonly; +>P : P +>T : T +>DeepReadonly : DeepReadonly +>T : T +>P : P + +}; + +type Foo = { +>Foo : Foo + + x: number; +>x : number + + y: { a: string, b: number }; +>y : { a: string; b: number; } +>a : string +>b : number + + z: boolean; +>z : boolean + +}; + +type DeepReadonlyFoo = { +>DeepReadonlyFoo : DeepReadonlyFoo + + readonly x: number; +>x : number + + readonly y: { readonly a: string, readonly b: number }; +>y : { readonly a: string; readonly b: number; } +>a : string +>b : number + + readonly z: boolean; +>z : boolean + +}; + +var x1: DeepReadonly; +>x1 : DeepReadonly +>DeepReadonly : DeepReadonly +>Foo : Foo + +var x1: DeepReadonlyFoo; +>x1 : DeepReadonly +>DeepReadonlyFoo : DeepReadonlyFoo + diff --git a/tests/cases/conformance/types/mapped/mappedTypes4.ts b/tests/cases/conformance/types/mapped/mappedTypes4.ts new file mode 100644 index 00000000000..74b3e395f32 --- /dev/null +++ b/tests/cases/conformance/types/mapped/mappedTypes4.ts @@ -0,0 +1,62 @@ +// @strictNullChecks: true +// @declaration: true + +type Box = { +}; + +type Boxified = { + [P in keyof T]: Box; +}; + +function boxify(obj: T): Boxified { + if (typeof obj === "object") { + let result = {} as Boxified; + for (let k in obj) { + result[k] = { value: obj[k] }; + } + return result; + } + return obj; +} + +type A = { a: string }; +type B = { b: string }; +type C = { c: string }; + +function f1(x: A | B | C | undefined) { + return boxify(x); +} + +type T00 = Partial; +type T01 = Readonly; +type T02 = Boxified +type T03 = Readonly; +type T04 = Boxified; +type T05 = Partial<"hello" | "world" | 42>; + +type BoxifiedWithSentinel = { + [P in keyof T]: Box | U; +} + +type T10 = BoxifiedWithSentinel; +type T11 = BoxifiedWithSentinel; +type T12 = BoxifiedWithSentinel; + +type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; + +type Foo = { + x: number; + y: { a: string, b: number }; + z: boolean; +}; + +type DeepReadonlyFoo = { + readonly x: number; + readonly y: { readonly a: string, readonly b: number }; + readonly z: boolean; +}; + +var x1: DeepReadonly; +var x1: DeepReadonlyFoo; \ No newline at end of file