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

Omit utility type throws away details of union types #49688

Closed
ducminh-phan opened this issue Jun 27, 2022 · 3 comments
Closed

Omit utility type throws away details of union types #49688

ducminh-phan opened this issue Jun 27, 2022 · 3 comments

Comments

@ducminh-phan
Copy link

Bug Report

πŸ”Ž Search Terms

omit union broaden types

πŸ•— Version & Regression Information

  • This is the behaviour in every version I tried, and I reviewed the FAQ for entries about union types.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Schedule =
  | {
      scheduled: true;
      scheduleTime: number;
    }
  | {
      scheduled: false;
      scheduleTime: null;
    };

type Item = {
  id: number;
  name: string;
} & Schedule;

// id is auto-generated
// 
// From inspection, CreateItemData is equivalent to
// type CreateItemData = {
//   name: string;
//   scheduled: boolean;
//   scheduleTime: number | null;
// }
type CreateItemData = Omit<Item, "id">;

const data: CreateItemData = {
  name: "item name",
  scheduled: true,
  scheduleTime: null,
};


// @ts-expect-error: This works correctly
const scheduleData: Schedule = {
  scheduled: true,
  scheduleTime: null,
};

πŸ™ Actual behavior

The line const data: CreateItemData = ... typechecks, as CreateItemData throws away some important data about Schedule type.

πŸ™‚ Expected behavior

The line const data: CreateItemData = ... should not typecheck, CreateItemData should be equal to

type CreateItemData =
  | {
      name: string;
      scheduled: true;
      scheduleTime: number;
    }
  | {
      name: string;
      scheduled: false;
      scheduleTime: null;
    };
@fatcerberus
Copy link

Omit does not distribute over unions. See #49659 (comment)

@ducminh-phan
Copy link
Author

Thanks for pointing me to #49659. @RyanCavanaugh Should we bring DistributedOmit into TS's core utility types?

@MartinJohns
Copy link
Contributor

Should we bring DistributedOmit into TS's core utility types?

See #39522 (comment):

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes.

Also #49656 (comment):

One of the reasons we don't like adding type aliases to the built-in lib -- we can't "upgrade" them!

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

No branches or pull requests

3 participants