Skip to content

Commit

Permalink
Add .pick and .omit (#51)
Browse files Browse the repository at this point in the history
This PR:

- [X] Adds `.pick` and `.omit` methods which work similarly to
`.extract` and `.exclude`.

---------

Co-authored-by: Misha Kaletsky <15040698+mmkal@users.noreply.github.com>
  • Loading branch information
aryaemami59 and mmkal committed Feb 26, 2024
1 parent 56c3682 commit 7085137
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ expectTypeOf<Customer | Employee>().extract<{foo: string}>().toBeNever()
expectTypeOf<Customer | Employee>().exclude<{name: string}>().toBeNever()
```

Use `.pick` to pick a set of properties from an object:

```typescript
type Person = {name: string; age: number}

expectTypeOf<Person>().pick<'name'>().toEqualTypeOf<{name: string}>()
```

Use `.omit` to remove a set of properties from an object:

```typescript
type Person = {name: string; age: number}

expectTypeOf<Person>().omit<'name'>().toEqualTypeOf<{age: number}>()
```

Make assertions about object properties:

```typescript
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ export interface BaseExpectTypeOf<Actual, Options extends {positive: boolean}> {
toBeConstructibleWith: Options['positive'] extends true ? (...args: ConstructorParams<Actual>) => true : never
extract: <V>(v?: V) => ExpectTypeOf<Extract<Actual, V>, Options>
exclude: <V>(v?: V) => ExpectTypeOf<Exclude<Actual, V>, Options>
pick: <K extends keyof Actual>(v?: K) => ExpectTypeOf<Pick<Actual, K>, Options>
omit: <K extends keyof Actual>(v?: K) => ExpectTypeOf<Omit<Actual, K>, Options>
parameter: <K extends keyof Params<Actual>>(number: K) => ExpectTypeOf<Params<Actual>[K], Options>
parameters: ExpectTypeOf<Params<Actual>, Options>
constructorParameters: ExpectTypeOf<ConstructorParams<Actual>, Options>
Expand Down Expand Up @@ -415,6 +417,8 @@ export const expectTypeOf: _ExpectTypeOf = <Actual>(
/* eslint-enable mmkal/@typescript-eslint/no-unsafe-assignment */
extract: expectTypeOf,
exclude: expectTypeOf,
pick: expectTypeOf,
omit: expectTypeOf,
toHaveProperty: expectTypeOf,
parameter: expectTypeOf,
}
Expand Down
12 changes: 12 additions & 0 deletions test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ test('`.extract` and `.exclude` return never if no types remain after exclusion'
expectTypeOf<Customer | Employee>().exclude<{name: string}>().toBeNever()
})

test('Use `.pick` to pick a set of properties from an object', () => {
type Person = {name: string; age: number}

expectTypeOf<Person>().pick<'name'>().toEqualTypeOf<{name: string}>()
})

test('Use `.omit` to remove a set of properties from an object', () => {
type Person = {name: string; age: number}

expectTypeOf<Person>().omit<'name'>().toEqualTypeOf<{age: number}>()
})

test('Make assertions about object properties', () => {
const obj = {a: 1, b: ''}

Expand Down

0 comments on commit 7085137

Please sign in to comment.