Closed
Description
Bug Report
π Search Terms
union generics
self-referential generic
ndarray
recursive type references
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
β― Playground Link
π» Code
type FooType<T> = T[] | T;
type BarType<T> = FooType<FooType<T>>;
const bar: BarType<number> = [];
const foo: FooType<number> = [3];
bar.push(foo);
π Actual behavior
When making use of an indirect self-referential union generic such as:
type FooType<T> = T[] | T;
type BarType<T> = FooType<FooType<T>>;
I'm unable to assign what I believe to be a reasonable pathway of the expansion
const bar: BarType<number> = [];
const foo: FooType<number> = [3];
bar.push(foo);
this should be the equivalent of FooType<number>[]
, however typescript disagrees that this assignment is valid
and only allows type number
Argument of type 'number[]' is not assignable to parameter of type 'number'.
I understand that the message is due to the number literal, but the typing is equivalent, as expressed
by the annotations.
Interestingly the single type version of this works, as demonstrated in (#33050)
type FooType<T> = T | FooType<T>[];
So it appears that the indirection is what's breaking this.
π Expected behavior
I'm able to compile the above without error
Metadata
Metadata
Assignees
Labels
No labels