question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`pipe` loses generics

See original GitHub issue

TypeScript Version: 3.4.1

Search Terms: generic rest parameters pipe compose

Code

I’m defining pipe using generic rest parameters as recommended here: https://github.com/Microsoft/TypeScript/issues/29904#issuecomment-471334674.

// Copied from https://github.com/Microsoft/TypeScript/issues/29904#issuecomment-471334674
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;

declare const myGenericFn: <T>(t: T) => string[];
declare const join: (strings: string[]) => string;

// Expected type: `<T>(t: T) => string`
// Actual type: `(t: any) => string`
const fn1 = pipe(
    myGenericFn,
    join,
);

// Workaround:
// Expected and actual type: `<T>(t: T) => string`
const fn2 = pipe(
    myGenericFn,
    strings => join(strings),
);

Playground Link: https://www.typescriptlang.org/play/index.html#src=declare function pipe<A extends any[]%2C B>(ab%3A (...args%3A A) %3D> B)%3A%20(…args%3A%20A)%20%3D%3E%20B%3B%0D%0Adeclare%20function%20pipe%3CA%20extends%20any%5B%5D%2C%20B%2C%20C%3E(%0D%0A%20%20%20%20ab%3A%20(…args%3A%20A)%20%3D%3E%20B%2C%0D%0A%20%20%20%20bc%3A%20(b%3A%20B)%20%3D%3E%20C%2C%0D%0A)%3A%20(…args%3A%20A)%20%3D%3E%20C%3B%0D%0Adeclare%20function%20pipe%3CA%20extends%20any%5B%5D%2C%20B%2C%20C%2C%20D%3E(%0D%0A%20%20%20%20ab%3A%20(…args%3A%20A)%20%3D%3E%20B%2C%0D%0A%20%20%20%20bc%3A%20(b%3A%20B)%20%3D%3E%20C%2C%0D%0A%20%20%20%20cd%3A%20(c%3A%20C)%20%3D%3E%20D%2C%0D%0A)%3A%20(…args%3A%20A)%20%3D%3E%20D%3B%0D%0A%0D%0Adeclare%20const%20myGenericFn%3A%20%3CT%3E(t%3A%20T)%20%3D%3E%20string%5B%5D%3B%0D%0Adeclare%20const%20join%3A%20(strings%3A%20string%5B%5D)%20%3D%3E%20string%3B%0D%0A%0D%0A%2F%2F%20Expected%20type%3A%20%60%3CT%3E(t%3A%20T)%20%3D%3E%20string%60%0D%0A%2F%2F%20Actual%20type%3A%20%60(t%3A%20any)%20%3D%3E%20string%60%0D%0Aconst%20fn1%20%3D%20pipe(%0D%0A%20%20%20%20myGenericFn%2C%0D%0A%20%20%20%20join%2C%0D%0A)%3B%0D%0A%0D%0A%2F%2F%20Workaround%3A%0D%0A%2F%2F%20Expected%20and%20actual%20type%3A%20%60%3CT%3E(t%3A%20T)%20%3D%3E%20string%60%0D%0Aconst%20fn2%20%3D%20pipe(%0D%0A%20%20%20%20myGenericFn%2C%0D%0A%20%20%20%20strings%20%3D%3E%20join(strings)%2C%0D%0A)%3B%0D%0A

Related Issues: https://github.com/Microsoft/TypeScript/issues/29904

/cc @ahejlsberg

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
babaknesscommented, Apr 12, 2019

Hey guys, for what its worth, I’ve come up with a recursive Pipe and Compose which preserves parameter names. I think this is a better experience than have variables names like a, b etc.

It tolerates generics better but this too has issues with generics.

Anyway, I just published this library

Type only:

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

Implementation:

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

and here is an article I wrote on it

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

1reaction
ahejlsbergcommented, Nov 12, 2019

Does the reasoning in this comment apply here too?

Yes, that reasoning applies to this example as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Introduction to Generics in Go | by Na'aman Hirschfeld
The use of the interface{} type causes the loss of typing information and type ... with the pipe (“|” ) symbol denoting the...
Read more >
Angular 2 - Custom Pipe with <T> to avoid losing intellisense ...
The pipe above will return an Array<any> , hence the intellisense on the view is completely lost. The real question. I was wondering...
Read more >
First generics to Bristol-Myers and Pfizer's Eliquis are here ...
Bristol and Pfizer have argued that's when Eliquis generics can enter. While the pair has reached settlements with several generics players that “do...
Read more >
Go Generics: Everything You Need To Know - Medium
Using the pipe operator, we've added our custom type CustomType to the printValue generic function type parameter's constraint. And now we have ...
Read more >
Creating a typed "compose" function in TypeScript
This post assumes you know at least a bit about generics in TypeScript. ... The partner of compose is pipe , which is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found