Get return type with reflect-metadata , when return type is Promise of something
Asked Answered
R

2

8

When function return type is Promise<number> , how can i get it with reflection?

If i just do

Reflect.getMetadata("design:returntype", target, key)

it's return just Promise, so there is a way to know that is Promise of number?

name:"Promise"
prototype:Promise {constructor: , then: , catch: , …}
reject:function reject() { … }
resolve:function resolve() { … }
Revivify answered 7/2, 2019 at 2:56 Comment(1)
I have the same question. :-/Karajan
K
0

It seems that this feature will not be supported in the near future.

As noted in #14971 (comment), reflection and runtime type serialization is outside the scope of the TypeScript project in the time being.

Mohamed Hegazy (link)

Karajan answered 27/1, 2020 at 13:48 Comment(0)
H
0

Try to use Custom Method Decorator ?

/* Important Notes

  • all type passed in the Decorator must be class
  • interface & enum are not supported */

(TypeScript)

export function ReturnType(type: any): MethodDecorator {
  return (target, propertyKey) => {
    Reflect.defineMetaData('YOUR_CUSTOM_KEY', type, target, propertyKey);
  }
}

// Somewhere in your code

@ReturnType(Number)
async function myFunc():Promise<number> {
  return await Promise.resolve(1234)
}
Heshvan answered 17/3, 2021 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.