假设我有已知类型A、B、C定义如下:
class A {
a!: string;
}
class B {
b!: string;
}
class C {
c!: string;
}
现在我定义了一个类型:
// 我希望这里的D能够自动推断
export type Root<D> = {
deps: D;
depsInstance: () => void
} & ThisType<{ depArray: D }>
声明变量指定类型:
// const root:Root<[A, B, C]> = {
// deps: [{ a: 1 },{ b: 2 },{ c: 3 }]
//}
// 我希望这里不需要传入泛型 `<[typeof A, typeof B, typeof C]>`
const root: Root<[typeof A, typeof B, typeof C]> = {
deps: [A, B, C], // 由这里来决定 泛型D 类型, 有点儿像类型反向推断
depsInstance: function () {
// 这里的this实际上是 { { depArray: [A的实例,B的实例,C的实例] }}
const [a, b, c] = this.depArray;
}
}
多看看自己的代码,你的 SomeType 可能类型就不对