Skip to content

Unable to assign self to indirect self-referential union genericΒ #50283

Description

@Seamooo

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

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

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions