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

Type inference for string literal union #10174

Closed
Galfour opened this issue Aug 5, 2016 · 1 comment
Closed

Type inference for string literal union #10174

Galfour opened this issue Aug 5, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Galfour
Copy link

Galfour commented Aug 5, 2016

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)

type dis = 'a' | 'b' | 'c';
type Cmap = Map<dis, number>;
var a: Cmap = new Map([['a', 2], ['b', 3]]);

type dis = 'a' | 'b' | 'c';
let a = 'a';
let b:dis = a;

Expected behavior:
inferred type of a is Map<dis, number>
inferred type of b is dis

Actual behavior:
inferred type of a is Map<string, number>
inferred type of b is string

Hello !
There is no map notation, so we have to use the Map constructor. However, it is not correctly typed, and requires type annotation () at every construction.
EDIT : The problem is not with Map. Cf, the other example.
It looks like the string union type is not propagated.
I found it to be expected when a is explicitely typed as 'string'. But when no annotation is given, I did not find the expected semantic.
As such, I do not know if it is a bug, or by design, but I did not find anything referring to that particular issue.

Have a good day !

@Galfour Galfour changed the title Type inference for Maps constructor Type inference for string disjunction Aug 5, 2016
@Galfour Galfour changed the title Type inference for string disjunction Type inference for string literal union Aug 5, 2016
@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 5, 2016
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 5, 2016

Hey @Galfour, thanks for filing, but this looks like a duplicate of #9489. We can continue the conversation there.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants