Skip to content

Commit

Permalink
Rename Omit to Except (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
kainiedziela and sindresorhus committed Jul 5, 2019
1 parent 5809b49 commit cfac1de
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export * from './source/basic';

// Utilities
export {Omit} from './source/omit';
export {Except} from './source/except';
export {Mutable} from './source/mutable';
export {Merge} from './source/merge';
export {MergeExclusive} from './source/merge-exclusive';
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ $ npm install type-fest
## Usage

```ts
import {Omit} from 'type-fest';
import {Except} from 'type-fest';

type Foo = {
unicorn: string;
rainbow: boolean;
};

type FooWithoutRainbow = Omit<Foo, 'rainbow'>;
type FooWithoutRainbow = Except<Foo, 'rainbow'>;
//=> {unicorn: string}
```

Expand All @@ -64,7 +64,7 @@ Click the type names for complete docs.

### Utilities

- [`Omit`](source/omit.d.ts) - Create a type from an object type without certain keys.
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type).
- [`Mutable`](source/mutable.d.ts) - Convert an object with `readonly` properties into a mutable object. Inverse of `Readonly<T>`.
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive properties.
Expand Down
22 changes: 22 additions & 0 deletions source/except.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
Create a type from an object type without certain keys.
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/30825) if you want to have the stricter version as a built-in in TypeScript.
@example
```
import {Except} from 'type-fest';
type Foo = {
a: number;
b: string;
c: boolean;
};
type FooWithoutA = Except<Foo, 'a' | 'c'>;
//=> {b: string};
```
*/
export type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
4 changes: 2 additions & 2 deletions source/merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Omit} from './omit';
import {Except} from './except';

/**
Merge two types into a new type. Keys of the second type overrides keys of the first type.
Expand All @@ -19,4 +19,4 @@ type Bar = {
const ab: Merge<Foo, Bar> = {a: 1, b: 2};
```
*/
export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
export type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
17 changes: 0 additions & 17 deletions source/omit.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions source/require-at-least-one.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Omit} from './omit';
import {Except} from './except';

/**
Create a type that requires at least one of the given properties. The remaining properties are kept as is.
Expand Down Expand Up @@ -29,4 +29,4 @@ export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = ke
)
}[KeysType]
// …then, make intersection types by adding the remaining properties to each mapped type.
& Omit<ObjectType, KeysType>;
& Except<ObjectType, KeysType>;
5 changes: 5 additions & 0 deletions test-d/except.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
import {Except} from '..';

declare const except: Except<{a: number; b: string}, 'b'>;
expectType<{a: number}>(except);
5 changes: 0 additions & 5 deletions test-d/omit.ts

This file was deleted.

0 comments on commit cfac1de

Please sign in to comment.