From ff2cfd2af5008e086470ac244442559a6febbb2e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 28 Feb 2017 09:49:54 -0800 Subject: [PATCH] Update test --- .../thisType/thisTypeInObjectLiterals2.ts | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals2.ts b/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals2.ts index 7358b47259c..d476679aef1 100644 --- a/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals2.ts +++ b/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals2.ts @@ -1,4 +1,5 @@ // @declaration: true +// @strictNullChecks: true // @noImplicitAny: true // @noImplicitThis: true // @target: es5 @@ -34,15 +35,19 @@ let obj1 = { type Point = { x: number; y: number; - moveBy(dx: number, dy: number): void; + z?: number; + moveBy(dx: number, dy: number, dz?: number): void; } let p1: Point = { x: 10, y: 20, - moveBy(dx, dy) { + moveBy(dx, dy, dz) { this.x += dx; this.y += dy; + if (this.z && dz) { + this.z += dz; + } } }; @@ -51,9 +56,12 @@ declare function f1(p: Point): void; f1({ x: 10, y: 20, - moveBy(dx, dy) { + moveBy(dx, dy, dz) { this.x += dx; this.y += dy; + if (this.z && dz) { + this.z += dz; + } } }); @@ -97,6 +105,51 @@ let x2 = makeObject2({ } }); +// Check pattern similar to Object.defineProperty and Object.defineProperties + +type PropDesc = { + value?: T; + get?(): T; + set?(value: T): void; +} + +type PropDescMap = { + [K in keyof T]: PropDesc; +} + +declare function defineProp(obj: T, name: K, desc: PropDesc & ThisType): T & Record; + +declare function defineProps(obj: T, descs: PropDescMap & ThisType): T & U; + +let p10 = defineProp(p1, "foo", { value: 42 }); +p10.foo = p10.foo + 1; + +let p11 = defineProp(p1, "bar", { + get() { + return this.x; + }, + set(value: number) { + this.x = value; + } +}); +p11.bar = p11.bar + 1; + +let p12 = defineProps(p1, { + foo: { + value: 42 + }, + bar: { + get(): number { + return this.x; + }, + set(value: number) { + this.x = value; + } + } +}); +p12.foo = p12.foo + 1; +p12.bar = p12.bar + 1; + // Proof of concept for typing of Vue.js type Accessors = { [K in keyof T]: (() => T[K]) | Computed };