Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Improve TypeScript example for conditional Omit usage - #130

#130
Open
huggingbot wants to merge 1 commit into
MetaMask:mainMetaMask/contributor-docs:mainfrom
huggingbot:refactor/typescript-examplehuggingbot/contributor-docs:refactor/typescript-exampleCopy head branch name to clipboard
Open

Improve TypeScript example for conditional Omit usage#130
huggingbot wants to merge 1 commit into
MetaMask:mainMetaMask/contributor-docs:mainfrom
huggingbot:refactor/typescript-examplehuggingbot/contributor-docs:refactor/typescript-exampleCopy head branch name to clipboard

Conversation

@huggingbot

Copy link
Copy Markdown
Member

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.