Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Losing generics (composition/currying) #12838

Closed
KiaraGrouwstra opened this issue Dec 11, 2016 · 2 comments
Closed

Losing generics (composition/currying) #12838

KiaraGrouwstra opened this issue Dec 11, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KiaraGrouwstra
Copy link
Contributor

I'm trying to work on TS typings for FP lib Ramda.
Several users have reported issues that came down to similar inference issues triggered in the presence of composition/currying involving functions containing generics.
Since FP encourages composing point-free functions (the majority of which use generics for type inference), what breaks here aren't so much edge cases, but idiomatic usage.

TypeScript Version: nightly (2.2.0-dev.20161211)

Code (can paste into the TS playground):

declare function inc(n: number): number; // +1
declare function identity<T>(a: T): T;
declare function compose<V0, T1>(fn0: (x0: V0) => T1): (x0: V0) => T1;
declare function compose<V0, T1, T2>(fn1: (x: T1) => T2, fn0: (x: V0) => T1): (x: V0) => T2;
declare function pipe   <V0, T1>(fn0: (x0: V0) => T1): (x0: V0) => T1;                           // arity 1: same as compose
declare function pipe   <V0, T1, T2>(fn0: (x: V0) => T1, fn1: (x: T1) => T2): (x: V0) => T2;     // arity 2: params swapped

// don't use generics if it can't resolve them right away:
compose(identity) // generics lost, now {} => {}
pipe   (identity) // ditto
// argument order apparently matters too:
pipe   (inc, identity); // ok, number -> number
compose(identity, inc); // nope, number => {}
// also no reasoning backward:
compose(inc, identity); // {} => number
pipe   (identity, inc); // {} => number

Expected behavior:

  • identity survives composition
  • identity not killing the type signature when composing with inc

Actual behavior:

  • Things break as soon as TS gets a generic it can't immediately resolve (as opposed to keeping them intact).
  • Even the same thing using different param order breaks for some reason.
@Igorbek
Copy link
Contributor

Igorbek commented Dec 13, 2016

That is possibly a use case for #10247
What you're probably looking for is a way to generically operate with generics.

@KiaraGrouwstra
Copy link
Contributor Author

Thanks for linking me, I'll follow that. You'll get all my upvotes on that, hope we can get to a solution.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 13, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants