basarat / basarat/typescript-book
Subtraction Types
- Dominant language
- TypeScript
- Stars
- 21.6k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Using a clever combination of `Pick` to create an `Omit`: (Source: https://github.com/Microsoft/TypeScript/issues/19569)
```
export type Diff = ({[P in T]: P} &
{[P in U]: never} & {[x: string]: never})[T]
export type Omit = Pick>
```
Better with conditional types : https://github.com/Microsoft/TypeScript/issues/19569#issuecomment-359471618
```
export type Diff = T extends U ? never : T; // Remove types from T that are assignable to U
export type Omit = Pick>
```
Contributor guide
Research direction
Review the TypeScript book’s existing coverage of Pick, Omit, and conditional types, then compare it with the two Diff/Omit examples and the linked TypeScript discussion. Done should be a clearly scoped decision about whether and where this material belongs, with the relevant explanation and examples updated if accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100