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
generic function to generic type
parameters< partial application
partial generic parameters
Suggestion
At the moment there seem to be two distinct kinds of generics: those which refer to types and those which refer to functions. It would be ideal if you could talk about the two interchangeably or get a generic type from a generic function.
Use Cases
This would allow us to reflect and/or copy the typings applied to functions using certain generics. In other words, we could reflect on the changes of a generic function's typings under different constraints in different parts of the code.
For example, it could be useful for factories which can combine multiple functions into a single pipeline.
Shortcomings of current approaches listed with examples below.
Another example, we could copy a function signature into another function with some of the generics partially applied / curried.
But this just makes F: any which is not helpful if we're trying to analyze the behavior of a function under certain generics.
Here's a larger, contrived example of how it might work in a larger context.
constfn=<Textendsany>({ id, hello } : {id: T,hello: T}): {id: T,hello: T}=>({ id, hello })constfn2=<Textendsany>({ id, some } : {id: T,some: T}): {id: T,some: T}=>({ id, some })constfns=[fn,fn2];constFactory=<Textendsany>(actions: {fn: (typeof<T>fns[number]),id: T,params: Parameters<typeof<T>fns[number]>[0],}[],cb: (params: Parameters<typeof<T>fns[number]>[0]): ReturnType<typeof<T>fns[number]>=>{cb(actions[0].fn({id: actions[0].id, ...actions[0].params}));});Factory([{fn: fns[0],id: "hello",params: {hello: "world"}},{fn: fns[1],id: 123,params: {some: 456}},{fn: fns[0],id: 123,params: {hello: "oops"// not allowed}}],({ id, ...result})=>{// We know every value of idif(id==="hello"){// we now know the contents of result}});
This is just one idea. It's kind of a rough interpretation of something I'm trying to do in a production application (where I combine various generic callbacks of a variety of different React components into a single one, and we identify the types of the parameters passed up the chain by the id parameter passed along with it).
I believe the linked issue is an exactish duplicate; you might want to review what's in there and post a comment chiming in with some additional use cases if any are missing. Thanks!
Search Terms
generic function to generic type
parameters< partial application
partial generic parameters
Suggestion
At the moment there seem to be two distinct kinds of generics: those which refer to types and those which refer to functions. It would be ideal if you could talk about the two interchangeably or get a generic type from a generic function.
Use Cases
This would allow us to reflect and/or copy the typings applied to functions using certain generics. In other words, we could reflect on the changes of a generic function's typings under different constraints in different parts of the code.
For example, it could be useful for factories which can combine multiple functions into a single pipeline.
Shortcomings of current approaches listed with examples below.
Another example, we could copy a function signature into another function with some of the generics partially applied / curried.
Examples
This would make
F
strictly typed to"hello"
At the moment, this isn't possible. The best we can do is something like:
But this just makes
F: any
which is not helpful if we're trying to analyze the behavior of a function under certain generics.Here's a larger, contrived example of how it might work in a larger context.
This is just one idea. It's kind of a rough interpretation of something I'm trying to do in a production application (where I combine various generic callbacks of a variety of different React components into a single one, and we identify the types of the parameters passed up the chain by the id parameter passed along with it).
There are of course many other potential uses.
It has been discussed previously on StackOverflow (not my post) https://stackoverflow.com/questions/62720954/typescript-how-to-create-a-generic-type-alias-for-a-generic-function
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: