-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Request: Infer generics #30887
Comments
I also faced this issue when I proposed new Curring interface to export type CurriedN<A extends Array<unknown>, B> = A extends []
? () => B
: A extends [unknown]
? ((...args: A) => B)
: ((...args: A) => unknown) extends ((head: infer H, ...tail: infer T) => unknown)
? ((...args: [H]) => CurriedN<T, B>)
: never However, this alias drops generics parameter. According to @gcanti , import { FunctionN } from 'fp-ts/lib/function'
export type CurriedN<A extends Array<unknown>, B> = A extends []
? () => B
: A extends [unknown]
? ((...args: A) => B)
: ((...args: A) => unknown) extends ((head: infer H, ...tail: infer T) => unknown)
? ((...args: [H]) => CurriedN<T, B>)
: never
declare function curry2<A extends Array<unknown>, B>(f: FunctionN<A, B>): CurriedN<A, B>
declare function map<A, B>(as: Array<A>, f: (a: A) => B): Array<B>
const mapCurried2 = curry2(map)
// inferred type: mapCurried2: (args_0: {}[]) => (f: (a: {}) => {}) => {}[] I also request this feature. Thank you. |
@RyanCavanaugh @babakness I confirm to work my example in v4.1.0-beta! We can close this issue. Cheers 🍻 |
@RyanCavanaugh @tani Hmm... this isn't working for me using playground for v4.1.0-beta. @tani Can you send me a link or provide instructions on how this is working for you? Otherwise, could we open this again? |
Thank you, I agree with you. I forget what I do that time. Next time, I keep my reproducible code. |
The request is to let
infer
support generics.Background:
I recently published a recursive
Pipe
andCompose
. It offers some key advantages, including the preservation of variable names.https://github.com/babakness/pipe-and-compose-types
Its shortcoming is in the case of generics. This is because
infer
does not work well with generics. ExampleTypeScript 3.4 improved how
pipe
andcompose
functions work with generics with higher order type inference from generic functions.Common implementations of
pipe
andcompose
rely on parameter overloading. However, even here, there is an issue with variadic functions. See example in this issue:#30727
The broader issue with the parameter overloading version of
pipe
andcompose
is that parameter names are lost, decreasing code clarity. The recursive version, relying oninfer
does maintain parameter names.Providing support generics with
infer
will present a major productivity bump and will nearly complete the community's need for the common placepipe
andcompose
in FP patterns without compromise.The only outlier is a small situation with parameter names on variadic functions of a minimum arity of zero as opposed to at least one. Can provide examples if needed since it is a separate issue.
The text was updated successfully, but these errors were encountered: