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.

Generics inside array doesn't determined correctly

See original GitHub issue

Bug Report

🔎 Search Terms

Generics inside array doesn’t determined correctly

🕗 Version & Regression Information

  • This changed between versions 3.8.3 and 3.9.7 - types inferred correctly if function argument is omited.
  • If the argument is present not working in any version.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface IPipe<I, O> { }
interface Pipeline<I, O> { }

class Bus<T> {
    pipe<A>(pipes: [IPipe<T, A>]): Pipeline<T, A>;
    pipe<A, B>(pipes: [IPipe<T, A>, IPipe<A, B>]): Pipeline<T, B>;
    pipe<A, B, C>(pipes: [IPipe<T, A>, IPipe<A, B>, IPipe<B, C>]): Pipeline<T, C>;
    pipe<A, B, C, D>(pipes: [IPipe<T, A>, IPipe<A, B>, IPipe<B, C>, IPipe<C, D>]): Pipeline<T, D>;
    pipe(pipes: IPipe<any, any>[]): Pipeline<any, any> {
        return null;
    }
}

function map<I, O>(mapper: (value: I) => O): IPipe<I, O> { return; }

const bus = new Bus<number>();

bus.pipe([
    map(() => ``),
    map(() => [true]),
    map(() => [``]),
    map(() => false)
]);

🙁 Actual behavior

Every next pipe first generic argument determined as Bus basic type.

bus.pipe(
   map((x) => ``),
     // ^ x: number
   map((x) => [true]),
     // ^ x: number
   map((x) => [``]),
     // ^ x: number
   map((x) => false)
     // ^ x: number
);

🙂 Expected behavior

Every next pipe first generic argument should be based on previous pipe second generic type.

bus.pipe(
   map((x) => ``),
     // ^ x: number
   map((x) => [true]),
     // ^ x: string
   map((x) => [``]),
     // ^ x: boolean[]
   map((x) => false)
     // ^ x: string[]
);

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Jan 13, 2022

Thanks, that’s much clearer.

Generic inference has a fixed number of passes for fixing type parameters and isn’t capable of inferring this sort of pattern.

See also #30134

0reactions
Flairaxcommented, Jan 12, 2022

@RyanCavanaugh, changed description in @whzx5byb way. Think it more clearer now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the reason I can't create generic array types in Java?
Arrays of generic types are not allowed because they're not sound. The problem is due to the interaction of Java arrays, which are...
Read more >
Why can't java generics be in arrays?
However it has this tricky problem that arrays like to know what type they are at runtime. However generics in Java is based...
Read more >
How To Simulate Generic Arrays In Java?
Answer: Arrays are covariant in Java i.e. any subclass array can be assigned to a supertype array. Generics, however, are invariant i.e. you ......
Read more >
Cannot create a generic array of ArrayList<Integer>
In a little more detail: The Java designers decided to disallow arrays of generics because 1) it would not be type-safe, and 2)...
Read more >
Creating a Generic Array in Java - Baeldung
An important difference between arrays and generics is how they enforce type checking. Specifically, arrays store and check type information at ...
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