简体   繁体   中英

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.

The closest we have currently is constructor-information for some decorators (properties, parameters, classes), but that's different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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