简体   繁体   中英

Is it possible to pass generic type as generic parameter?

Just pass generic type like a callback :

type FUNC<ARG,RET, F> = (arg: ARG) => F<RET>;
type PROMISE<T> = Promise<T>;
type IDENT<T> = T;
type A = FUNC<number, void, IDENT>;
type A_PROMISE = FUNC<number, void, PROMISE>;

Is this possible?

It is not possible at the moment. There was a discussion back in 2014. See this issue .


If your intention is to constraint the return type, you may:

type FUNC<ARG,RET extends Promise<any>> = (arg: ARG) => RET;

To get back the wrapped type, you may:

type PromiseType<T> = T extends Promise<infer T> ? T : never

type A = Promise<string>
type B = PromiseType<A> // string

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