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

Improve TypeScript example for conditional Omit usage #130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

huggingbot
Copy link

Description

This PR updates the TypeScript documentation with a clearer example of using Omit with conditional types. The new example makes it easier to understand how to conditionally remove properties based on the value of quantity.

Changes

  • Replaced the previous singleItemPayload example with a more precise generic type SingleItemPayload<T>.
  • Improved the explanation and updated the example to show how color is omitted when quantity === 1.

Before

const singleItemPayload = Omit<CartItem, "color" extends string ? "color" : never>;
const cartPayload: singleItemPayload[] = [];

After

type SingleItemPayload<T extends CartItem> = T extends { quantity: 1 }
  ? Omit<T, 'color'>
  : T

const cartPayload: SingleItemPayload<CartItem & { quantity: 1 }>[] = [
  { productId: 123, quantity: 1 },
];

@huggingbot huggingbot requested a review from a team as a code owner February 26, 2025 04:52
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

Successfully merging this pull request may close these issues.

1 participant