Skip to content

Commit

Permalink
adding tests for casting the type of a memoized function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed Oct 20, 2021
1 parent d8246db commit e5a4d8b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/types-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,46 @@ it('should return a memoized function that satisies a typeof check for the origi
expectTypeOf<typeof result>().toEqualTypeOf<number>();
});

it('should allow casting back to the original function type', () => {
type AddFn = (first: number, second: number) => number;
function add(first: number, second: number): number {
return first + second;
}
// baseline
{
const memoized = memoize(add);
expectTypeOf<typeof memoized>().toEqualTypeOf<AddFn>();
expectTypeOf<typeof memoized>().toEqualTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().toEqualTypeOf<MemoizedFn<AddFn>>();
expectTypeOf<typeof memoized>().toMatchTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().toMatchTypeOf<MemoizedFn<AddFn>>();
}
{
const memoized: typeof add = memoize(add);
expectTypeOf<typeof memoized>().toEqualTypeOf<AddFn>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<AddFn>>();
}
{
const memoized: AddFn = memoize(add);
expectTypeOf<typeof memoized>().toEqualTypeOf<AddFn>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<AddFn>>();
}
{
const memoized = memoize(add) as typeof add;
expectTypeOf<typeof memoized>().toEqualTypeOf<AddFn>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<AddFn>>();
}
{
const memoized = memoize(add) as AddFn;
expectTypeOf<typeof memoized>().toEqualTypeOf<AddFn>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<typeof add>>();
expectTypeOf<typeof memoized>().not.toMatchTypeOf<MemoizedFn<AddFn>>();
}
});

it('should type the equality function to based on the provided function', () => {
function add(first: number, second: number) {
return first + second;
Expand Down

0 comments on commit e5a4d8b

Please sign in to comment.