Skip to content

Commit

Permalink
feat(types): Added StrictOmit, FN types
Browse files Browse the repository at this point in the history
  • Loading branch information
YLfjuk committed Sep 27, 2024
1 parent a348648 commit 8375596
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/types/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export type ExtractValues<T> = T extends object
? T
: ExtractValues<ValueOf<T>>
: T;

export type StrictOmit<T, K extends keyof T> = Omit<T, K>;

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type FN<Return = any, Args = any> = (...args: Args[]) => Return;
41 changes: 41 additions & 0 deletions packages/types/tests/fn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { FN } from '../src';
import { describe, test, expectTypeOf } from 'vitest';

describe('FN', () => {
test('any function type', () => {
// biome-ignore lint/suspicious/noExplicitAny: testing
const fn = (...args: any[]) => {
return args?.[0];
};

type Expected = FN;

type Actual = typeof fn;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('any any[] function type', () => {
// biome-ignore lint/suspicious/noExplicitAny: testing
const fn = (...args: any[]) => {
return args;
};

// biome-ignore lint/suspicious/noExplicitAny: testing
type Expected = FN<any[]>;

type Actual = typeof fn;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('string void function type', () => {
const fn = (...args: string[]) => console.log(args);

type Expected = FN<void, string>;

type Actual = typeof fn;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
});
19 changes: 19 additions & 0 deletions packages/types/tests/strict-omit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { StrictOmit } from '../src';
import { describe, test, expectTypeOf } from 'vitest';

describe('Strict Omit', () => {
test('type omit keys of object from object', () => {
const obj = {
Bob: 'bob',
Bert: 'bert',
};

type Expected = {
Bob: string;
};

type Actual = StrictOmit<typeof obj, 'Bert'>;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
});

0 comments on commit 8375596

Please sign in to comment.