Skip to content

Commit

Permalink
Except: Add another example to JSDoc (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
som-sm authored Jan 15, 2025
1 parent 879d8ae commit db12bb1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions source/except.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
// The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
// Consider the following example:
type UserData = {
[metadata: string]: string;
email: string;
name: string;
role: 'admin' | 'user';
};
// `Omit` clearly doesn't behave as expected in this case:
type PostPayload = Omit<UserData, 'email'>;
//=> type PostPayload = { [x: string]: string; [x: number]: string; }
// In situations like this, `Except` works better.
// It simply removes the `email` key while preserving all the other keys.
type PostPayload = Except<UserData, 'email'>;
//=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
```
@category Object
Expand Down

0 comments on commit db12bb1

Please sign in to comment.