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

Is it possible to Omit native type like booleans or numbers? #653

Closed
silversonicaxel opened this issue Jul 28, 2023 · 3 comments
Closed

Comments

@silversonicaxel
Copy link

silversonicaxel commented Jul 28, 2023

I would like to know if it is possible to have a scenario like this one:

type A = {
  a: string;
  b: number;
  c: boolean;
  d: boolean;
  e: boolean;
};

type B = Omit<A, boolean>;

where type B looks like this

type B = {
  a: string;
  b: number;
};

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@tommy-mitchell
Copy link
Contributor

ConditionalExcept should work:

type B = ConditionalExcept<A, boolean>;

@silversonicaxel
Copy link
Author

Amazing, last part of question is, what if one boolean is optional?
Because now, considering this example:

type A = {
  a: string;
  b: number;
  c: boolean;
  d?: boolean;
  e: boolean;
};

type B = ConditionalExcept <A, boolean>;

type B looks like this:

type B = {
  a: string;
  b: number;
  d?: boolean;
};

@tommy-mitchell
Copy link
Contributor

d?: boolean is the same as d: boolean | undefined (for most cases). One solution is to use the built-in Required utility type (TS Playground):

type B = ConditionalExcept<Required<A>, boolean>;

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