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

Suggestion: recursive pipe / compose types #822

Closed
babakness opened this issue Apr 12, 2019 · 3 comments
Closed

Suggestion: recursive pipe / compose types #822

babakness opened this issue Apr 12, 2019 · 3 comments

Comments

@babakness
Copy link
Contributor

Hi, I did some work recently on the Pipe and Compose types. I have an approach that uses a recursive type to deliver better typings on composed functions. For example, parameter names are preserved.

Here is a link the repo with the types

https://github.com/babakness/pipe-and-compose-types

Here is a link to the implementation:

https://github.com/babakness/pipe-and-compose

Related article:

https://dev.to/babak/introducing-the-recursive-pipe-and-compose-types-3g9o

--

There is good reason to keep the these types in an external repo. TypeScript's compiler seems treat them better that way.

All the best.

@gcanti
Copy link
Owner

gcanti commented Apr 12, 2019

@babakness thanks for sharing

p.s.

FYI Looks like there's some issue with polymorphic functions

import { head } from 'fp-ts/lib/Array'
import { pipe } from 'fp-ts/lib/function'
import { lift } from 'fp-ts/lib/Functor'
import { option } from 'fp-ts/lib/Option'

const toPair = <A>(a: A): [A, A] => [a, a]

const liftedToPair = lift(option)(toPair)

// f: <A>(a: A[]) => Option<[A, A]>
const f = pipe(
  head,
  liftedToPair
)

import { pipe as pipe2 } from 'pipe-and-compose'

// f2: (as: {}[]) => Option<[{}, {}]>
const f2 = pipe2(head, liftedToPair)

@babakness
Copy link
Contributor Author

@gcanti thanks for noticing this. I see that as of the latest TS 3.4+, overloading pipe and compose without variadic support are now supported by the compiler. Variadic support is not there:

microsoft/TypeScript#30727

I believe the support needed by the compiler for a recursive version such as mine is for infer to work with generics. The recursive type depends on these helpers

/**
 * Extracts function arguments
 */
export type ExtractFunctionArguments < Fn > = Fn extends  ( ...args: infer P ) => any  ? P : never

/**
 * Extracts function return values
 */
export type ExtractFunctionReturnValue<Fn> = Fn extends  ( ...args: any[] ) => infer P  ? P : never

I'll keep an eye out for this. I created a feature request for this at TypeScript's repo:

microsoft/TypeScript#30887

@gcanti gcanti closed this as completed May 17, 2019
@EricCrosson
Copy link
Contributor

The linked issue microsoft/TypeScript#30887 closed 8 days ago 👀

Does this mean we're unblocked in terms of offering a compose function with fp-ts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants