Skip to content

Commit

Permalink
feat(#83): finish OmitByType<T,P>
Browse files Browse the repository at this point in the history
  • Loading branch information
ashgw committed Apr 23, 2024
1 parent c9a1189 commit ba431c4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

Collection of utility types, decorators and helper functions to bullet proof TypeScript even more.

[![release](https://github.com/AshGw/ts-roids/actions/workflows/ci.yml/badge.svg)](https://github.com/AshGw/ts-roids/actions/workflows/ci.yml)
[![tests](https://github.com/AshGw/ts-roids/actions/workflows/test.yml/badge.svg)](https://github.com/AshGw/ts-roids/actions/workflows/test.yml)
[![CI](https://github.com/AshGw/ts-roids/actions/workflows/ci.yml/badge.svg)](https://github.com/AshGw/ts-roids/actions/workflows/ci.yml)
[![@latest](https://img.shields.io/npm/v/ts-roids.svg)](https://www.npmjs.com/package/ts-roids)
[![npm downloads](https://img.shields.io/npm/dm/ts-utils.svg)](https://www.npmjs.com/package/ts-roids)
[![bundle size](https://img.shields.io/bundlephobia/minzip/utility-types.svg)](https://www.npmjs.com/package/ts-roids)
Expand Down
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,39 @@ export type FilterBy<T, P> = {
[K in Keys<T>]: K extends P ? K : never;
}[Keys<T>];

/**
* Get a set of properties from `T` whose type are not assignable to `P`.
* @example
* ````ts
* type T = {
* foo: string,
* bar: bigint | boolean,
* baz: number,
* }
* OmitByType<T,true>; // Result: T
* OmitByType<T,number>; // Result: { foo: string, bar: bigint | boolean }
* ````
*/
export type OmitByType<T, P> = {
[K in Keys<T> as T[K] extends P ? never : K]: T[K];
};

/**
* From ``T``, pick a set of properties whose type are assignable to ``P``.
* @example
* ````ts
* type T = {
* foo: string,
* bar: bigint | boolean,
* baz: number,
* }
* PickByType<T,true>; // Result: {}
* PickByType<T,number>; // Result: { baz: number }
* ````
*/
export type PickByType<T, P> = {
[K in Keys<T> as T[K] extends P ? K : never]: T[K];
};
export type Stretch<T> = T extends object
? T extends infer P
? { [K in Keys<P>]: Stretch<P[K]> }
Expand Down
46 changes: 46 additions & 0 deletions tests/omit-by-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Numeric, Nullable, OmitByType, TestType } from 'src';
import { test, expect } from 'vitest';

type OneLevelDeep = {
foo: boolean;
bar?: Numeric;
baz: Nullable;
fooBaz: bigint;
bazFoo: string | boolean;
};

test('_', () => {
const result: TestType<
OmitByType<OneLevelDeep, bigint>,
{
foo: boolean;
bar?: Numeric;
baz: Nullable;
bazFoo: string | boolean;
},
true
> = true;
expect(result).toBe(true);
});

test('_', () => {
const result: TestType<
OmitByType<OneLevelDeep, string | boolean>,
{
bar?: Numeric;
baz: Nullable;
fooBaz: bigint;
},
true
> = true;
expect(result).toBe(true);
});

test('_', () => {
const result: TestType<
OmitByType<OneLevelDeep, true>,
OneLevelDeep,
true
> = true;
expect(result).toBe(true);
});
42 changes: 42 additions & 0 deletions tests/pick-by-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Numeric, Nullable, EmptyObject, PickByType, TestType } from 'src';
import { test, expect } from 'vitest';

type OneLevelDeep = {
foo: boolean;
bar?: Numeric;
baz: Nullable;
fooBaz: bigint;
bazFoo: string | boolean;
};

test('_', () => {
const result: TestType<
PickByType<OneLevelDeep, bigint>,
{
fooBaz: bigint;
},
true
> = true;
expect(result).toBe(true);
});

test('_', () => {
const result: TestType<
PickByType<OneLevelDeep, string | boolean>,
{
foo: boolean;
bazFoo: string | boolean;
},
true
> = true;
expect(result).toBe(true);
});

test('_', () => {
const result: TestType<
PickByType<OneLevelDeep, true>,
EmptyObject,
true
> = true;
expect(result).toBe(true);
});

0 comments on commit ba431c4

Please sign in to comment.