4 ms·
I've been bashing my head trying to create some sort of typed pipe for a while, with a syntax matching Elixirs, to avoid having to create curried functions all
by svrtknst 4y ago
I've been bashing my head trying to create some sort of typed pipe for a while, with a syntax matching Elixirs, to avoid having to create curried functions all the time. The farthest I've gotten is that I achieve typing, so long as the function isn't generic. So this works:
const plus = (a: number, b: number) => a + b;
pipe(5)
.thru(plus, 3) // Pipe(8)
// ^---- this is inferred as `number`
But this doesn't
function plus<T extends string|number>(a: T, b: T) {/* ... */}
pipe("five")
.thru(plus, "three") // Pipe("fivethree")
// ^--- this isnt inferred or narrowed,becoms `unknown` iirc
- deleted 4y ago[deleted]
- SebastianKra 4y agoHave you tried this: function doSth<A, B>(a: A, b: B): B { /* ... */ } function callFn<Args extends any[], Out>(fn: (...args: Args) => Out, ...args: Args): Out { /* ... */ } const foo = callFn(doSth, 'asdf', 'bsd' as const) // a: 'bsd' const bar = callFn(doSth, 'asdf') // error
- svrtknst 4y agoI've tried something similar, but splitting the args into Head and Tail and extending the Parameters<F> of the input function and some other trickery Been a little while since I looked at it tho