Generics inside array doesn't determined correctly
See original GitHub issueBug 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:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
@RyanCavanaugh, changed description in @whzx5byb way. Think it more clearer now.