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

Wrong union type matching #15907

Closed
dfilatov opened this issue May 17, 2017 · 2 comments · Fixed by #30779
Closed

Wrong union type matching #15907

dfilatov opened this issue May 17, 2017 · 2 comments · Fixed by #30779
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dfilatov
Copy link

dfilatov commented May 17, 2017

TypeScript Version: 2.3.2

Code

type Action = { type: 'activate' } | { type: 'disactivate' };

function dispatchAction(action: Action): void {

}

const active = true;

dispatchAction({ type : (active? 'disactivate' : 'activate') });

Expected behavior:
Everything is correct. And if I change last line to:

dispatchAction(active? { type : 'disactivate' } : { type : 'activate' });

then no errors will be produced.

Actual behavior:
Error:
Argument of type '{ type: "activate" | "disactivate"; }' is not assignable to parameter of type 'Action'.
Type '{ type: "activate" | "disactivate"; }' is not assignable to type '{ type: "disactivate"; }'.
Types of property 'type' are incompatible.
Type '"activate" | "disactivate"' is not assignable to type '"disactivate"'.
Type '"activate"' is not assignable to type '"disactivate"'.

@ahejlsberg
Copy link
Member

This is working as intended. I would suggest writing the declaration of type Action as follows to clearly indicate that the type property has two possible values regardless of the rest of the properties:

type Action = { type: 'activate' | 'disactivate' };

In general, the unification you propose is only possible for a small subset of types. See my comment in #12052.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 17, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 2, 2017

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

@mhegazy mhegazy closed this as completed Jun 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

Successfully merging a pull request may close this issue.

3 participants