Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make type unions work with generics #21120

Closed
mertdeg2 opened this issue Jan 10, 2018 · 3 comments
Closed

Make type unions work with generics #21120

mertdeg2 opened this issue Jan 10, 2018 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mertdeg2
Copy link

mertdeg2 commented Jan 10, 2018

TypeScript Version: all

Code

type foo = string|number;

class Baz<T extends foo> {
  x: T = 'a';
}

Expected behavior:
I expected to be able to assign something to x. (As my coworkers explained to me) In this case, you can't assign anything to x, because "T extends foo" means T is subtype of foo (Not the other way around).

Second Expected behavior:
Given above, I think it would be nice if TS allowed something like:

type foo = string|number;

class Baz<foo extends T> {
  x: T = 'a';
}

Or maybe something like below (which i heard is supported in Java)?

type foo = string|number;

class Baz<foo super T> {
  x: T = 'a';
}
@mhegazy
Copy link
Contributor

mhegazy commented Jan 10, 2018

The first example is wrong as you already noted. Baz<number> is a valid instantiation, and thus x: number = "a" is invalid.
The second is tracked by #14520

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 10, 2018
@mertdeg2
Copy link
Author

Thanks @mhegazy

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants