From 3cf236a37431389c96a884ff1d8397e467157a51 Mon Sep 17 00:00:00 2001 From: mmkal Date: Mon, 14 Dec 2020 13:27:48 +0000 Subject: [PATCH] Make type tests more future-proof --- test/types.ts | 85 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/test/types.ts b/test/types.ts index 2d0c515..693c951 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,7 +1,8 @@ import test from 'ava'; -import {expectTypeOf} from 'expect-type'; +import {ExpectTypeOf, expectTypeOf} from 'expect-type'; import {TypedArray} from 'type-fest'; import ow, {BasePredicate} from '../source'; +import predicates from '../source/predicates'; test('type-level tests', t => { t.is(typeof typeTests, 'function'); @@ -90,41 +91,57 @@ function typeTests(value: unknown) { }, () => { - // For the rest of the validators, just make sure they're mapped to the correct type. - // This helper makes it easy to write a one-line type validation. - const ensure = (predicate: BasePredicate): T => { - ow(value, predicate); - return value; + // To make sure all validators are mapped to the correct type, create a `Tests` type which requires that + // every property of `ow` has its type-mapping explicitly tested. If more properties are added this will + // fail until a type assertion is added below. + + type AssertionProps = Exclude; + + type Tests = { + [K in AssertionProps]: + typeof ow[K] extends BasePredicate + ? (type: ExpectTypeOf) => void + : never + }; + + const tests: Tests = { + array: expect => expect.toBeArray(), + arrayBuffer: expect => expect.toEqualTypeOf(), + boolean: expect => expect.toBeBoolean(), + buffer: expect => expect.toEqualTypeOf(), + dataView: expect => expect.toEqualTypeOf(), + date: expect => expect.toEqualTypeOf(), + error: expect => expect.toEqualTypeOf(), + float32Array: expect => expect.toEqualTypeOf(), + float64Array: expect => expect.toEqualTypeOf(), + function: expect => expect.toEqualTypeOf(), + int16Array: expect => expect.toEqualTypeOf(), + int32Array: expect => expect.toEqualTypeOf(), + int8Array: expect => expect.toEqualTypeOf(), + iterable: expect => expect.toEqualTypeOf>(), + map: expect => expect.toEqualTypeOf>(), + nan: expect => expect.toEqualTypeOf(Number.NaN), + null: expect => expect.toEqualTypeOf(), + nullOrUndefined: expect => expect.toEqualTypeOf(), + number: expect => expect.toBeNumber(), + object: expect => expect.toBeObject(), + promise: expect => expect.toEqualTypeOf>(), + regExp: expect => expect.toEqualTypeOf(), + set: expect => expect.toEqualTypeOf>(), + sharedArrayBuffer: expect => expect.toEqualTypeOf(), + string: expect => expect.toBeString(), + symbol: expect => expect.toEqualTypeOf(), + typedArray: expect => expect.toEqualTypeOf(), + uint16Array: expect => expect.toEqualTypeOf(), + uint32Array: expect => expect.toEqualTypeOf(), + uint8Array: expect => expect.toEqualTypeOf(), + uint8ClampedArray: expect => expect.toEqualTypeOf(), + undefined: expect => expect.toEqualTypeOf(), + weakMap: expect => expect.toEqualTypeOf>(), + weakSet: expect => expect.toEqualTypeOf>() }; - expectTypeOf(ensure(ow.arrayBuffer)).toEqualTypeOf(); - expectTypeOf(ensure(ow.buffer)).toEqualTypeOf(); - expectTypeOf(ensure(ow.dataView)).toEqualTypeOf(); - expectTypeOf(ensure(ow.date)).toEqualTypeOf(); - expectTypeOf(ensure(ow.error)).toEqualTypeOf(); - expectTypeOf(ensure(ow.float32Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.float64Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.function)).toEqualTypeOf(); - expectTypeOf(ensure(ow.int16Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.int32Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.map)).toEqualTypeOf>(); - expectTypeOf(ensure(ow.int8Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.nan)).toEqualTypeOf(Number.NaN); - expectTypeOf(ensure(ow.null)).toEqualTypeOf(); - expectTypeOf(ensure(ow.nullOrUndefined)).toEqualTypeOf(); - expectTypeOf(ensure(ow.sharedArrayBuffer)).toEqualTypeOf(); - expectTypeOf(ensure(ow.promise)).toEqualTypeOf>(); - expectTypeOf(ensure(ow.regExp)).toEqualTypeOf(); - expectTypeOf(ensure(ow.set)).toEqualTypeOf>(); - expectTypeOf(ensure(ow.symbol)).toEqualTypeOf(); - expectTypeOf(ensure(ow.typedArray)).toEqualTypeOf(); - expectTypeOf(ensure(ow.uint16Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.uint32Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.uint8Array)).toEqualTypeOf(); - expectTypeOf(ensure(ow.uint8ClampedArray)).toEqualTypeOf(); - expectTypeOf(ensure(ow.undefined)).toEqualTypeOf(); - expectTypeOf(ensure(ow.weakMap)).toEqualTypeOf>(); - expectTypeOf(ensure(ow.weakSet)).toEqualTypeOf>(); + return tests; } ]; }