Truly variadic pipe type #234
Replies: 6 comments
-
|
I'm not sure I see what's this about, without any practical examples. The |
Beta Was this translation helpful? Give feedback.
-
|
I quote the code here: The pipe operation typings only support strict type checking up to 10 operations, after which nesting the invocations or unknown types have to be accepted. My code is certainly not perfect (working on removing the any types as we speak) but the possibility of supporting any number of operations while preserving type checking was intriguing enough in my opinion to ask about it. |
Beta Was this translation helpful? Give feedback.
-
|
Pipes here support recursion and extension, so the chances of 10 operators being not enough are too small. i.e. you can pipe a stream, and then re-pipe it indefinitely, or you can have sub-pipes. And there's hardly any real-world example where 10 operators would not be enough. I've never seen one. |
Beta Was this translation helpful? Give feedback.
-
|
Since I have a discussion to post from, and even though this probably won't get anywhere close to reasonable for a couple reasons I'll explain later, Here's a streamlined version, applied to pipeSync specifically: This version type-checks correctly with a notable exception. The return type is inferred to be never if the types of the operators mismatch. Regardless, due to this limitation this approach probably won't get far without this feature. On the subject of practicality, absolutely. There are near to none cases where this would be useful where simply recursing or nesting wouldn't be appropriate. I did it just for fun I guess. I apologize for going straight to an issue nevertheless, should've started with the discussion. Thanks for humoring me regardless. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your contribution! This project was fully finished some time ago, as production-ready, and at this point I can only allocate time for bug fixing. In the meantime, see iter-ops-extras for many custom operators.
That proposal is now 6 years old, so I wouldn't hold your breath on this. |
Beta Was this translation helpful? Give feedback.
-
|
I didn't mean to close it, but chances of further development in this direction are slim to none. I can only address bugs at this point, or add something into iter-ops-extras. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Has anyone here considered using recursive/conditional/inferred types to allow typescript to statically type the pipe function in a truly variadic way?
Here's an example of what's possible, applied to simple function composition, if only requiring some casts to achieve, but in exchange allowing for infinitely large truly variadic pipe:
This typechecks in typescript 5.4.5
To clarify my point, despite using
anyeverywhere in the type definition, this makes the pipe function be truly variadic, using type recursion. Ensuring type safety between the return type of the previous function and parameter of the nextfunction, using conditional and inferred types. The case where the length of the function tuple is not known is covered by a default(x: any) => anyin the last branch, but this could be changed to anything.The only real downside I can see to this is probably my poorly-performing pipe function implementation, which can be changed to the existing definition if needed, and lacking support for older typescript versions that don't have these features.
I'm sure a similar approach could be used for Operators, as defined in this library.
I can start work on it if the author deems it useful enough.
Beta Was this translation helpful? Give feedback.
All reactions