Skip to content

Commit d7908d1

Browse files
committed
Add tests
1 parent 671f7a8 commit d7908d1

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,53 @@ function f74(func: <T, U, K extends keyof (T | U)>(x: T, y: U, k: K) => (T | U)[
250250
let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean
251251
}
252252

253+
function f80<T extends { a: { x: any } }>(obj: T) {
254+
let a1 = obj.a; // { x: any }
255+
let a2 = obj['a']; // { x: any }
256+
let a3 = obj['a'] as T['a']; // T["a"]
257+
let x1 = obj.a.x; // any
258+
let x2 = obj['a']['x']; // any
259+
let x3 = obj['a']['x'] as T['a']['x']; // T["a"]["x"]
260+
}
261+
262+
function f81<T extends { a: { x: any } }>(obj: T) {
263+
return obj['a']['x'] as T['a']['x'];
264+
}
265+
266+
function f82() {
267+
let x1 = f81({ a: { x: "hello" } }); // string
268+
let x2 = f81({ a: { x: 42 } }); // number
269+
}
270+
271+
function f83<T extends { [x: string]: { x: any } }, K extends keyof T>(obj: T, key: K) {
272+
return obj[key]['x'] as T[K]['x'];
273+
}
274+
275+
function f84() {
276+
let x1 = f83({ foo: { x: "hello" } }, "foo"); // string
277+
let x2 = f83({ bar: { x: 42 } }, "bar"); // number
278+
}
279+
280+
class C1 {
281+
x: number;
282+
get<K extends keyof this>(key: K) {
283+
return this[key];
284+
}
285+
set<K extends keyof this>(key: K, value: this[K]) {
286+
this[key] = value;
287+
}
288+
foo() {
289+
let x1 = this.x; // number
290+
let x2 = this["x"]; // number
291+
let x3 = this.get("x"); // this["x"]
292+
let x4 = getProperty(this, "x"); // this["x"]
293+
this.x = 42;
294+
this["x"] = 42;
295+
this.set("x", 42);
296+
setProperty(this, "x", 42);
297+
}
298+
}
299+
253300
// Repros from #12011
254301

255302
class Base {
@@ -354,4 +401,22 @@ interface Options2<Data, Computed> {
354401
declare class Component2<Data, Computed> {
355402
constructor(options: Options2<Data, Computed>);
356403
get<K extends keyof Data | keyof Computed>(key: K): (Data & Computed)[K];
357-
}
404+
}
405+
406+
// Repro from #12651
407+
408+
type MethodDescriptor = {
409+
name: string;
410+
args: any[];
411+
returnValue: any;
412+
}
413+
414+
declare function dispatchMethod<M extends MethodDescriptor>(name: M['name'], args: M['args']): M['returnValue'];
415+
416+
type SomeMethodDescriptor = {
417+
name: "someMethod";
418+
args: [string, number];
419+
returnValue: string[];
420+
}
421+
422+
let result = dispatchMethod<SomeMethodDescriptor>("someMethod", ["hello", 35]);

0 commit comments

Comments
 (0)