You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I imagine this has to have come up before, but I couldn't find an exact duplicate. #28655 seems maybe related.
Basically, imagine two types:
typeA<T>=(value: T)=>TtypeB=<T>(value: T)=>T
With A, I can easily do:
typeA1=A<"hi">// (value: "hi") => "hi", as expected.typeA2=ReturnType<A1>// "hi", as expected
With B, there seems to be no syntax I can use to bind the type variable:
typeB1=B<"hi">// error, type B is not generictypeB2=ReturnType<B1>// impossible, because of error above
This becomes an issue in practice when using typeof:
functionit<T>(value: T){returnvalue;}typeC=typeofit;// Gives B, not A, understandably
Is there a workaround here? If not, I guess I'm suggesting that there be one. The current error more or less makes sense because, in the general case, I see that writing B<...> couldn't (easily) work to bind the inner type variables, as B could be something like:
But, in the original case, where B is <T extends string>(it: T) => any, maybe there could be some operator that I could use to "lift up" the generic to turn B into A and support this case?
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Thanks @RyanCavanaugh. I always really appreciate your quick, helpful responses. I'm gonna close this as a duplicate of #17574 (and also probably #29043 and a number of other related issues).
Search Terms
generic type function type typeof
Question/Suggestion
I imagine this has to have come up before, but I couldn't find an exact duplicate. #28655 seems maybe related.
Basically, imagine two types:
With
A
, I can easily do:With
B
, there seems to be no syntax I can use to bind the type variable:This becomes an issue in practice when using
typeof
:Is there a workaround here? If not, I guess I'm suggesting that there be one. The current error more or less makes sense because, in the general case, I see that writing
B<...>
couldn't (easily) work to bind the inner type variables, as B could be something like:But, in the original case, where
B
is<T extends string>(it: T) => any
, maybe there could be some operator that I could use to "lift up" the generic to turn B into A and support this case?Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: