Closed
Description
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?