我有一种更新嵌套对象属性的方法:
import * as set from 'lodash/fp/set';
class Foo {
private object: any;
updateDeepObjectProperty<T, K1 extends keyof T, K2 extends keyof T[K1]>(objectType: T, path: [K1, K2], value: T[K1][K2]);
updateDeepObjectProperty<T, K1 extends keyof T>(objectType: T, path: [K1], value: T[K1]);
updateDeepObjectProperty<T>(objectType: T, path: any, value: any) {
this.object = set(path, value, this.object);
}
}
interface ObjectInterface {
deep: {
property: number;
}
}
class Bar {
private foo = new Foo();
private objectType: ObjectInterface;
onUpdateThing(value: number): void {
this.foo.updateDeepObjectProperty(this.objectType, ['deep', 'property'], value);
}
}
我试图通过重载来推断K ...,而不必传递objectType参数,但打字稿抱怨缺少类型参数。
有什么方法可以添加ObjectInterface作为类型参数,但可以继续推断K1 ...?