-
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
Various issues when trying to use pipe
function
#29904
Comments
@OliverJAsh Appreciate the write-up. See #30114 for an improvement that will allow you to use a generic rest parameter to represent the parameter list of the first function argument passed to Other issues remain that we are aware of, such as support for inferring higher-order functions (the first issue) and issues relating to ordering of inferences when piping through combinations of contextually sensitive and contextually insensitive arguments (e.g. combinations of arrow functions and declared functions). We continue to contemplate possible solutions. |
With #30114 and #30193 all but issue 1 (inferring higher order function types) and issue 5 (overload resolution in inference and type relationships) above are now fixed. I'm definitely eager to address higher order function type inference (tracked by #9366) and I think we're getting closer to a workable solution. |
Higher order function type inference now implemented in #30215. |
With all these PRs merged, can we call this closed? |
😮. Thank you so much, this is going to make a massive difference. I'm not sure if all of the issues I listed above are fixed, so I'll defer to @ahejlsberg. |
Presuming these fixes are now merged and released in 1, 4, 6 are now working as expected. 🎉 (Thank you!) 2, 3, 5 however do not seem to have changed. IIUC, 5 is not expected to be fixed yet, but what about 2 and 3? |
The fix for 2 and 3 is to change your declarations of declare function pipe<A extends any[], B>(ab: (...args: A) => B): (...args: A) => B;
declare function pipe<A extends any[], B, C>(ab: (...args: A) => B, bc: (b: B) => C): (...args: A) => C;
declare function pipe<A extends any[], B, C, D>(ab: (...args: A) => B, bc: (b: B) => C, cd: (c: C) => D): (...args: A) => D;
// ... These declarations will more accurately capture the parameter list of the first function and will work for any number of parameters in the first function. |
Instead of writing overloads, we can write pipe & compose generically. |
@ahejlsberg Regarding issue 2 ("Incorrect overload is used for pipe"), is there any way to fix this without generic rest parameters, i.e. in some de-sugared form? I'm asking because I'd like to apply it to Lodash's The first thing that came to mind was to switch the order of the overloads, but this breaks other examples: declare const pipe: {
<A, B>(a: () => A, ab: (a: A) => B): () => B;
<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;
};
// 1
{
// Expected type: `(a: {} | undefined) => number`
const fn = pipe(
(_a?: {}) => 1,
x => x,
);
}
// 2
{
declare const identity: <T>(t: T) => T;
// Expected type: `<T>(t: T) => T`
const fn2 = pipe(
identity,
value => identity(value),
);
}
// 3
{
declare const getString: () => string;
// Expected type: `() => string`
const fn1 = pipe(
getString,
x => x,
);
} Out of the examples above, 1 (with declare const pipe: {
<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;
<A, B>(a: () => A, ab: (a: A) => B): () => B;
}; Generic rest parameters fixes all examples, but as I mentioned, this isn't currently an option that's available to us. declare const pipe: {
<A extends any[], B>(ab: (...args: A) => B): (...args: A) => B;
<A extends any[], B, C>(ab: (...args: A) => B, bc: (b: B) => C): (...args: A) => C;
}; |
I think I've ran into another bug (using 3.4.1). Posted here: #30727. |
Plug: I created a small library just for |
@ahejlsberg I have two new
|
@OliverJAsh's declare function pipe<A, B, C>(
ab: (this: void, a: A) => B,
bc: (this: void, b: B) => C,
): (arg: A) => C;
declare function pipeReverse<A, B, C>(
bc: (this: void, b: B) => C,
ab: (this: void, a: A) => B,
): (arg: A) => C;
type Box<T> = {val: T};
type Bag1 = {a: boolean; b: string};
type Bag2 = {c: number; d: object};
type Bag3 = {e: 'e val'; f: 'f val'};
declare function withBag2<T>(b: Box<T>): Box<T & Bag2>;
declare function withBag3<T>(b: Box<T>): Box<T & Bag3>;
const startingValue: Box<{starting: 'value'}> = {val: {starting: 'value'}};
// Piping from left to right works.
const baggedViaPipe = pipe(withBag2, withBag3)(startingValue);
// Piping from right to left does not.
//
// Argument of type '<T>(b: Box<T>) => Box<T & Bag3>' is not assignable to parameter of type '(this: void, a: unknown) => Box<T>'.
// Types of parameters 'b' and 'a' are incompatible.
// Type 'unknown' is not assignable to type 'Box<T>'.(2345)
const baggedViaPipeRev = pipeReverse(withBag2, withBag3)(startingValue); |
The specific sub-issues people have encountered which remain have their own sub-issues at this point - I'm going to close this issue in favor of them. |
TypeScript Version: 3.3.1
Search Terms: pipe compose functional programming composition generics overloads
Code
It goes without saying that
pipe
is a very common utility function for composing functions, used often in functional programming. I've been using apipe
function in TypeScript for about 2 years, and over time I've collected numerous issues.I wanted to create an issue to collect all of this information, to help others who want to use
pipe
so they are aware of the various footguns—and also for the TypeScript team to help their visibility of these issues.To the best of my ability, I have narrowed these bugs down to the simplest examples, and provided any interesting (and sometimes useful) workarounds. I would appreciate any help in narrowing these examples further—perhaps even combining them where perceived issues are artefacts of the same underlying problems.
In my experience, 80% of the time
pipe
just works. These issues cover the remaining 20% of the time. Issue 1 is by far the most significant. The rest are unordered.I have linked to sub issues where I'm aware they exist. For those without linked issues, we may want to create new issues to trick them independently.
1. Generics are lost when first composed function is generic
Related issues:
With the option suggested in #27288, TypeScript would at least alert the developer to change the code to workaround this problem.
2. Incorrect overload is used for
pipe
strictFunctionTypes
is disabledpipe
overload is zero parameters for first functionRelated issues: #29913
3. Inference does not work when first
pipe
overload is zero parameters for first functionRelated issues:
4. Untitled
Related issues:
pipe
is incorrect when strict mode is disabled #25826pipe
is incorrect when strict mode is disabled #25826 (comment)5. Incorrect overload is used for composed function
Related issues:
pipe
#25637pipe
#25637 (comment)6. Untitled
Related issues:
pipe
#25791pipe
#25791 (comment)The text was updated successfully, but these errors were encountered: