We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
When I use discriminating unions I expect that only one of them will be allowed by TS, not their intersection
Discriminating union
Playground link with relevant code
type Common = {foo: string}; type A = Common & {a: string}; type B = Common & {b: string}; type Params = Common | A | B; // expect error here const bar: Params = {foo: '123', a: '123', b: '123'}; const myFn = (params: Params) => {} // expect error here myFn({foo: '123', a: '123', b: '123'})
TS allows using both fields from A and B types
TS should treat this as an error
Also if I try to set never for fields that I don't want to be set, it doesn't work
never
type A = Common & {a: string, b: never}; type B = Common & {a: never, b: string};
There's a workaround for this, but it looks hacky
type A = Common & {a: string, b?: never}; type B = Common & {a: never, b: string};
The text was updated successfully, but these errors were encountered:
Duplicate of #20863. And you also want #12936.
Additional properties is simply something you have to expect and deal with if you want to write robust code, given the current design of the language.
Sorry, something went wrong.
@MartinJohns thank you.
For those who are looking for an answer:
type Common = {foo: string}; type A = {a: string, b?: never}; type B = {a?: never, b: string}; type C = {a?: never, b?: never}; type Params = Common & ( A | B | C);
No branches or pull requests
Bug Report
When I use discriminating unions I expect that only one of them will be allowed by TS, not their intersection
🔎 Search Terms
Discriminating union
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TS allows using both fields from A and B types
🙂 Expected behavior
TS should treat this as an error
Also if I try to set
never
for fields that I don't want to be set, it doesn't workThere's a workaround for this, but it looks hacky
The text was updated successfully, but these errors were encountered: