首页 > 解决方案 > 在 Typescript 中推断同级的类型

问题描述

我想在打字稿中实现这一点

const configuration: ResolveDeclaration = {
 deps: [Foo, Bar, Baz],
 resolveFn: (foo, bar, baz) => {} //<== foo, bar, baz has a type inherited in `deps` property accordingly
}

我尝试了这种方法:

export interface ResolvableLiteral<D extends readonly Injectable[]> {
  deps: D;
  resolveFn: (...args: D) => any;
}

但这显然不起作用,因为它不会推断传递的值,而是将其视为Injectable[]

更新:

稍后会以这种方式使用此配置:

const injectables = configuration.deps.map((token) => this.injector.get(token))
return configuration.resolveFn(...injectables)

因此,resolveFn我想获取从依赖注入器中询问的依赖类型。

标签: typescript

解决方案


推荐阅读