Skip to content

Commit

Permalink
maxBy and minBy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed Jan 3, 2024
1 parent fe88deb commit 1bbfa6a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/maxBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expectError, expectType } from 'tsd';

import { __, maxBy, prop } from '../es';

type Obj = {
str: string;
date: Date;
bool: boolean;
};

// please note how literals work in this situation
expectType<1 | 2>(maxBy(Math.abs, 1, 2));
// using variables that are type `number` is the more general use-case here
const a: number = 1;
const b: number = 2;
expectType<number>(maxBy(Math.abs, a, b));
// so can also upcast inline to get the same result
expectType<number>(maxBy(Math.abs, 1 as number, 2 as number));
// now check the other Ord types
expectType<Obj>(maxBy(prop('str'), {} as Obj, {} as Obj));
expectType<Obj>(maxBy(prop('bool'), {} as Obj, {} as Obj));
expectType<Obj>(maxBy(prop('date'), {} as Obj, {} as Obj));

// Placeholder
// expectType<number>(max(__, b)(a));
expectType<number>(maxBy(__, 1 as number, 2 as number)(Math.abs));
// curried
// notice how literals work fine here because `T` is pulled directly from Math.abs
// in the full function typescript will hard union them, but here it cannot
expectType<number>(maxBy(Math.abs)(1)(2));

// don't allow different types
expectError(maxBy(Math.abs, 1, '2'));
33 changes: 33 additions & 0 deletions test/minBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expectError, expectType } from 'tsd';

import { __, minBy, prop } from '../es';

type Obj = {
str: string;
date: Date;
bool: boolean;
};

// please note how literals work in this situation
expectType<1 | 2>(minBy(Math.abs, 1, 2));
// using variables that are type `number` is the more general use-case here
const a: number = 1;
const b: number = 2;
expectType<number>(minBy(Math.abs, a, b));
// so can also upcast inline to get the same result
expectType<number>(minBy(Math.abs, 1 as number, 2 as number));
// now check the other Ord types
expectType<Obj>(minBy(prop('str'), {} as Obj, {} as Obj));
expectType<Obj>(minBy(prop('bool'), {} as Obj, {} as Obj));
expectType<Obj>(minBy(prop('date'), {} as Obj, {} as Obj));

// Placeholder
// expectType<number>(max(__, b)(a));
expectType<number>(minBy(__, 1 as number, 2 as number)(Math.abs));
// curried
// notice how literals work fine here because `T` is pulled directly from Math.abs
// in the full function typescript will hard union them, but here it cannot
expectType<number>(minBy(Math.abs)(1)(2));

// don't allow different types
expectError(minBy(Math.abs, 1, '2'));

0 comments on commit 1bbfa6a

Please sign in to comment.