简体   繁体   English

是否可以在打字稿中返回类型或接口?

[英]Is it possible to return a type or interface in typescript?

What I want is to return a interface, not a variable with the interface type.我想要的是返回一个接口,而不是具有接口类型的变量。 For example:例如:

Interface A{}

function f1(){
    return A;
}

No, I'm afraid this is not possible, because interfaces do not exist at runtime.不,恐怕这是不可能的,因为接口在运行时不存在。

What you are asking for would be in the domain of a theoretical reflection/type-introspection system injected into the Javascript runtime environment by the TypeScript compiler, perhaps with the returned interface converted to a runtime accessible metadata information object, but no such thing exists at this time.您所要求的将是在由 TypeScript 编译器注入到 Javascript 运行时环境中的理论反射/类型内省系统的领域中,可能将返回的接口转换为运行时可访问的元数据信息对象,但在这次。

The closest we have currently is constructor-information for some decorators (properties, parameters, classes), but that's different.我们目前最接近的是一些装饰器(属性、参数、类)的构造函数信息,但这是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM