From d7908d19be028650f3d68e7f27f3c7b22a76bf13 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 6 Dec 2016 14:42:08 -0800 Subject: [PATCH] Add tests --- .../types/keyof/keyofAndIndexedAccess.ts | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts index 3a2bb912c19..c7e9934bb5f 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts @@ -250,6 +250,53 @@ function f74(func: (x: T, y: U, k: K) => (T | U)[ let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean } +function f80(obj: T) { + let a1 = obj.a; // { x: any } + let a2 = obj['a']; // { x: any } + let a3 = obj['a'] as T['a']; // T["a"] + let x1 = obj.a.x; // any + let x2 = obj['a']['x']; // any + let x3 = obj['a']['x'] as T['a']['x']; // T["a"]["x"] +} + +function f81(obj: T) { + return obj['a']['x'] as T['a']['x']; +} + +function f82() { + let x1 = f81({ a: { x: "hello" } }); // string + let x2 = f81({ a: { x: 42 } }); // number +} + +function f83(obj: T, key: K) { + return obj[key]['x'] as T[K]['x']; +} + +function f84() { + let x1 = f83({ foo: { x: "hello" } }, "foo"); // string + let x2 = f83({ bar: { x: 42 } }, "bar"); // number +} + +class C1 { + x: number; + get(key: K) { + return this[key]; + } + set(key: K, value: this[K]) { + this[key] = value; + } + foo() { + let x1 = this.x; // number + let x2 = this["x"]; // number + let x3 = this.get("x"); // this["x"] + let x4 = getProperty(this, "x"); // this["x"] + this.x = 42; + this["x"] = 42; + this.set("x", 42); + setProperty(this, "x", 42); + } +} + // Repros from #12011 class Base { @@ -354,4 +401,22 @@ interface Options2 { declare class Component2 { constructor(options: Options2); get(key: K): (Data & Computed)[K]; -} \ No newline at end of file +} + +// Repro from #12651 + +type MethodDescriptor = { + name: string; + args: any[]; + returnValue: any; +} + +declare function dispatchMethod(name: M['name'], args: M['args']): M['returnValue']; + +type SomeMethodDescriptor = { + name: "someMethod"; + args: [string, number]; + returnValue: string[]; +} + +let result = dispatchMethod("someMethod", ["hello", 35]); \ No newline at end of file