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 behaviour #15778

Closed
dfilatov opened this issue May 11, 2017 · 7 comments
Closed

Wrong union behaviour #15778

dfilatov opened this issue May 11, 2017 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@dfilatov
Copy link

dfilatov commented May 11, 2017

TypeScript Version: 2.3.2

Code

interface Action<Type> {
    type: Type;
}

interface ActionWithPayload<Type, Payload> extends Action<Type> {
    payload: Payload;
}

type MyAction = Action<'type1'> | ActionWithPayload<'type2', number>;

const action: MyAction = {
    type: 'type1',
    payload: 3
};

Expected behavior:
I'd expect to see an error about unexpected payload property. And if I use only one type from union I'll get it:

const action: Action<'type1'> = {
    type: 'type1',
    payload: 3
};

Object literal may only specify known properties, and 'payload' does not exist in type 'Action<"type1">'.

Actual behavior:
There is no any errors

@mhegazy
Copy link
Contributor

mhegazy commented May 11, 2017

duplicate of #10575

looks like you are looking for #14094

A workaround would be to define your MyAction as:

type MyAction = ActionWithPayload<'type1', undefined> | ActionWithPayload<'type2', number>;

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 11, 2017
@dfilatov
Copy link
Author

@mhegazy Yes, exclusive unions would solve this. Thank you.

@dfilatov
Copy link
Author

dfilatov commented May 11, 2017

@mhegazy If I use suggested workaround, I can't do anymore:

const action: MyAction = {
    type: 'type1'    
};

I have to do:

const action: MyAction = {
    type: 'type1',
    payload: undefined
};

@mhegazy
Copy link
Contributor

mhegazy commented May 11, 2017

that is correct. consider setting payload?: undefined instead:

type MyAction = { type: "type1", payload?: undefined } | { type: "type2", payload: number };

@dfilatov
Copy link
Author

Cool! This works.

@AJamesPhillips
Copy link

AJamesPhillips commented May 12, 2017

This works like a charm. Just use it as discussed here: DefinitelyTyped/DefinitelyTyped#16416 (comment) Thanks @mhegazy !

@mhegazy
Copy link
Contributor

mhegazy commented May 30, 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 May 30, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants