Skip to content

Using Omit weakens type checking #46361

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
Bastian opened this issue Oct 14, 2021 · 3 comments
Closed

Using Omit weakens type checking #46361

Bastian opened this issue Oct 14, 2021 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Bastian
Copy link

Bastian commented Oct 14, 2021

Bug Report

When using the Omit type, the type-checking does not catch some cases that it previously did.

πŸ”Ž Search Terms

Omit inaccurate

πŸ•— Version & Regression Information

Test in both 3.3.3 and 4.5.0-beta.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface A {
  type: "a";
  payload: string;
  something: string;
}

interface B {
  type: "b";
  payload: boolean;
  something: string;
}

type AB = A | B;

type ABwithoutSomething = Omit<AB, "something">;

const a1: ABwithoutSomething = {
  type: "a",
  payload: true, // This should not work, but it does (unexpected)
};

const a2: AB = {
  type: "a",
  payload: true, // Without the Omit, the compiler complains here (expected)
  something: "",
};

πŸ™ Actual behavior

When using the Omit type to exclude a single field, the type checking is weaker.

πŸ™‚ Expected behavior

When using Omit, the only difference should be that the field is excluded without any other side-effects or weaker typechecks.

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 14, 2021

You expect the omit type to operate distributive, but it doesn't. Look up "distributive omit". You should look at the definition of omit to understand how it works and why it behaves the way it does.

@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 14, 2021
@Bastian
Copy link
Author

Bastian commented Oct 14, 2021

I see. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants