Skip to content

Unions accept extra keys with incorrect properties #17046

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

Closed
luke-john opened this issue Jul 9, 2017 · 2 comments
Closed

Unions accept extra keys with incorrect properties #17046

luke-john opened this issue Jul 9, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@luke-john
Copy link

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

interface SinglePropA { a: 'test-a' }
interface SinglePropB { b: 'test-b' }
type AorB =
    | SinglePropA
    | SinglePropB

const testAandB: AorB = {
    a: 'test-a',
    b: 'test-bs'
} // passes on 2.4.1 and 2.5.0-dev.20170629 

const testAandC: AorB = {
    a: 'test-a',
    c: 'test-b'
} // fails on 2.4.1 and 2.5.0-dev.20170629
/*
    Object literal may only specify known properties,
    and 'c' does not exist in type 'SubsetAnimal'.
*/

typescript playground example

Expected behavior:

Unions don't accept extra properties where the key matches another member of the union.

Actual behavior:

Unions accept extra properties where the key matches another member of the union but the value is incorrect.

Note. Came across this when trying to find a workaround for the following issue #17003

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 10, 2017
@RyanCavanaugh
Copy link
Member

See discussion in #16679

@luke-john
Copy link
Author

Thanks for that.

@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

2 participants