mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Add tests
This commit is contained in:
@@ -250,3 +250,45 @@ class OtherPerson {
|
||||
return getProperty(this, "parts")
|
||||
}
|
||||
}
|
||||
|
||||
// Modified repro from #12544
|
||||
|
||||
function path<T, K1 extends keyof T>(obj: T, key1: K1): T[K1];
|
||||
function path<T, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, key1: K1, key2: K2): T[K1][K2];
|
||||
function path<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3];
|
||||
function path(obj: any, ...keys: (string | number)[]): any;
|
||||
function path(obj: any, ...keys: (string | number)[]): any {
|
||||
let result = obj;
|
||||
for (let k of keys) {
|
||||
result = result[k];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
type Thing = {
|
||||
a: { x: number, y: string },
|
||||
b: boolean
|
||||
};
|
||||
|
||||
|
||||
function f1(thing: Thing) {
|
||||
let x1 = path(thing, 'a'); // { x: number, y: string }
|
||||
let x2 = path(thing, 'a', 'y'); // string
|
||||
let x3 = path(thing, 'b'); // boolean
|
||||
let x4 = path(thing, ...['a', 'x']); // any
|
||||
}
|
||||
|
||||
// Repro from comment in #12114
|
||||
|
||||
const assignTo2 = <T, K1 extends keyof T, K2 extends keyof T[K1]>(object: T, key1: K1, key2: K2) =>
|
||||
(value: T[K1][K2]) => object[key1][key2] = value;
|
||||
|
||||
// Modified repro from #12573
|
||||
|
||||
declare function one<T>(handler: (t: T) => void): T
|
||||
var empty = one(() => {}) // inferred as {}, expected
|
||||
|
||||
type Handlers<T> = { [K in keyof T]: (t: T[K]) => void }
|
||||
declare function on<T>(handlerHash: Handlers<T>): T
|
||||
var hashOfEmpty1 = on({ test: () => {} }); // {}
|
||||
var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean }
|
||||
@@ -120,4 +120,29 @@ function f10(foo: Foo) {
|
||||
let x = validate(foo); // { a: number, readonly b: string }
|
||||
let y = clone(foo); // { a?: number, b: string }
|
||||
let z = validateAndClone(foo); // { a: number, b: string }
|
||||
}
|
||||
}
|
||||
|
||||
// Repro from #12606
|
||||
|
||||
type Func<T> = (...args: any[]) => T;
|
||||
type Spec<T> = {
|
||||
[P in keyof T]: Func<T[P]> | Spec<T[P]> ;
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a spec object recursively mapping properties to functions, creates a function
|
||||
* producing an object of the same structure, by mapping each property to the result
|
||||
* of calling its associated function with the supplied arguments.
|
||||
*/
|
||||
declare function applySpec<T>(obj: Spec<T>): (...args: any[]) => T;
|
||||
|
||||
// Infers g1: (...args: any[]) => { sum: number, nested: { mul: string } }
|
||||
var g1 = applySpec({
|
||||
sum: (a: any) => 3,
|
||||
nested: {
|
||||
mul: (b: any) => "n"
|
||||
}
|
||||
});
|
||||
|
||||
// Infers g2: (...args: any[]) => { foo: { bar: { baz: boolean } } }
|
||||
var g2 = applySpec({ foo: { bar: { baz: (x: any) => true } } });
|
||||
Reference in New Issue
Block a user