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

Missing excess property error for discriminated union #39050

Open
OliverJAsh opened this issue Jun 12, 2020 · 1 comment
Open

Missing excess property error for discriminated union #39050

OliverJAsh opened this issue Jun 12, 2020 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Jun 12, 2020

TypeScript Version: 3.9.5

Search Terms:

Code

type Params = ({ shouldApplyStyles?: true } | { shouldApplyStyles: false }) &
    (
        | {
              internal: false;
          }
        | {
              internal: true;
              internalProp: string;
          }
    );

declare const fn: (params: Params) => void;

fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: true,
});
fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: false,
});
fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: undefined,
});
fn({
    internal: false,
    internalProp: 'foo', // Expected error but got none ❌
});

Expected behavior: See code comments

Actual behavior: See code comments

Playground Link: Link

Related Issues:

I looked at #20863 but I don't think this is exactly the same, because in this case we have a tagged aka discriminated union, whereas that issue describes behaviour for unions without tags aka discriminant properties.

@OliverJAsh OliverJAsh changed the title Missing excess property error for tagged union Missing excess property error for discriminated union Jun 12, 2020
@RyanCavanaugh
Copy link
Member

Formatted to apply my own mental model:

type Params =
    ({ shouldApplyStyles: false } | { shouldApplyStyles?: true })
    &
    ({ internal: false; } | { internal: true; internalProp: string; });

declare const fn: (params: Params) => void;

fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: true,
});
fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: false,
});
fn({
    internal: false,
    internalProp: 'foo', // ✅ Errors as expected
    shouldApplyStyles: undefined,
});
fn({
    internal: false,
    internalProp: 'foo', // Expected error but got none ❌
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants