From 1bbfa6a08afc56d1609ac38992395aabb5f57513 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Sun, 8 Oct 2023 15:56:52 -0600 Subject: [PATCH] maxBy and minBy tests --- test/maxBy.test.ts | 33 +++++++++++++++++++++++++++++++++ test/minBy.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/test/maxBy.test.ts b/test/maxBy.test.ts index e69de29b..bdf2c05e 100644 --- a/test/maxBy.test.ts +++ b/test/maxBy.test.ts @@ -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(maxBy(Math.abs, a, b)); +// so can also upcast inline to get the same result +expectType(maxBy(Math.abs, 1 as number, 2 as number)); +// now check the other Ord types +expectType(maxBy(prop('str'), {} as Obj, {} as Obj)); +expectType(maxBy(prop('bool'), {} as Obj, {} as Obj)); +expectType(maxBy(prop('date'), {} as Obj, {} as Obj)); + +// Placeholder +// expectType(max(__, b)(a)); +expectType(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(maxBy(Math.abs)(1)(2)); + +// don't allow different types +expectError(maxBy(Math.abs, 1, '2')); diff --git a/test/minBy.test.ts b/test/minBy.test.ts index e69de29b..cc39459e 100644 --- a/test/minBy.test.ts +++ b/test/minBy.test.ts @@ -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(minBy(Math.abs, a, b)); +// so can also upcast inline to get the same result +expectType(minBy(Math.abs, 1 as number, 2 as number)); +// now check the other Ord types +expectType(minBy(prop('str'), {} as Obj, {} as Obj)); +expectType(minBy(prop('bool'), {} as Obj, {} as Obj)); +expectType(minBy(prop('date'), {} as Obj, {} as Obj)); + +// Placeholder +// expectType(max(__, b)(a)); +expectType(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(minBy(Math.abs)(1)(2)); + +// don't allow different types +expectError(minBy(Math.abs, 1, '2'));