推断TypeScript中的构造函数类型

时间:2019-04-26 11:16:23

标签: typescript conditional-types

是否可以在TypeScript中推断类的构造函数类型? 我试过了,但似乎不起作用:

> Task :jaxbJavaGenName
[ant:xjc] [ERROR] Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/inheritance". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?
[ant:xjc]   line 9 of file:/opt/tep-gradle/component/core/tres/src/main/resources/xsd/ManagementConsoleChangePasswordRequest.xsd
[ant:xjc]
[ant:xjc] [ERROR] Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/inheritance". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?
[ant:xjc]   line 9 of file:/opt/tep-gradle/component/core/tres/src/main/resources/xsd/ManagementConsoleSignOnRequest.xsd
[ant:xjc]
[ant:xjc] [ERROR] Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/inheritance". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?
[ant:xjc]   line 9 of file:/opt/tep-gradle/component/core/tres/src/main/resources/xsd/ManagementConsoleVerifyMFARequest.xsd
[ant:xjc]

2 个答案:

答案 0 :(得分:0)

您可以通过类的构造函数来引用类类型,而不是尝试进行推断吗?

type Constructor<K> = { new(): K };

const x: Constructor<String> = String; 

const s = new x();

答案 1 :(得分:0)

已经有一个预定义的条件类型,它允许您从名为InstanceType的类类型中提取实例类型。

class A { private x: any}

type AInstance = InstanceType<typeof A> // same as A

此类型的定义是:

type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;