Skip to content

Excess properties in tagged union not checked if type is optional and literal #31655

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 3.4.1, 3.5.1

Search Terms: tagged union optional literal excess properties props

Code

type Button = { tag: 'button'; type?: 'submit'; };
type Anchor = { tag: 'a'; type?: string; href: string };

type Union = Button | Anchor;
const obj: Union = {
    tag: 'button',
    type: 'submit',

    // no error, bad! :-(
    href: 'foo',
    // error, good! :-)
    href2: 'foo',
};

If I replace Button with:

type Button = { tag: 'button'; type?: string };

That is, if I replace the optional string literal with an optional string, then it works as expected (I get an error for the excess prop href), suggesting this is down to the fact that we're using string literals?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions