From f734c701709099f38560076ac0983cc17166e150 Mon Sep 17 00:00:00 2001 From: Kharazyan Aram Date: Sat, 14 Oct 2023 11:12:05 +0400 Subject: [PATCH] fix(propEq): improve propEq typings * remove unnecessary constraint from propEq value: the function should receive any type values of object, no matter what parameter it received; * add additional types to use with placeholder --- test/anyPass.test.ts | 2 +- test/propEq.test.ts | 114 ++++++++++++++++++++++++++++++++++++++----- types/propEq.d.ts | 36 ++++++++++++-- 3 files changed, 133 insertions(+), 19 deletions(-) diff --git a/test/anyPass.test.ts b/test/anyPass.test.ts index ff46aa58..b3f96319 100644 --- a/test/anyPass.test.ts +++ b/test/anyPass.test.ts @@ -25,7 +25,7 @@ expectType( }) ); -expectError( +expectType( isVampire({ age: 21, garlic_allergy: true, diff --git a/test/propEq.test.ts b/test/propEq.test.ts index dc5b9098..7a6f6c8a 100644 --- a/test/propEq.test.ts +++ b/test/propEq.test.ts @@ -1,39 +1,127 @@ import { expectError, expectType } from 'tsd'; -import { propEq } from '../es'; +import {__, propEq} from '../es'; type Obj = { union: 'foo' | 'bar'; str: string; - num: number; + int: number; + numLike: number | `${number}`; + optional?: string; + nullable: string | null; u: undefined; n: null; }; +type NumArr = number[]; +// ###################### // propEq(val, name, obj) expectType(propEq('foo', 'union', {} as Obj)); -// non-union string fails -expectError(propEq('nope', 'union', {} as Obj)); -// completely different type fails -expectError(propEq(2, 'union', {} as Obj)); +// propEq doesn't create unnecessary values constraint for object +expectType(propEq('1', 'union', {} as Obj)); +// union of number with literal types should work fine +expectType(propEq(1, 'numLike', {} as Obj)); +expectType(propEq('1', 'numLike', {} as Obj)); +// optional types doesn't fire an error, if passed correct types +expectType(propEq('str', 'optional', {} as Obj)); +expectType(propEq(undefined, 'optional', {} as Obj)); +// fires an error only on wrong type +expectError(propEq(1, 'optional', {} as Obj)); +expectError(propEq(null, 'optional', {} as Obj)); +// nullable types doesn't fire an error, if passed correct types +expectType(propEq('str', 'nullable', {} as Obj)); +expectType(propEq(null, 'nullable', {} as Obj)); +// fires an error only on wrong type +expectError(propEq(1, 'nullable', {} as Obj)); +expectError(propEq(undefined, 'nullable', {} as Obj)); +// unknown field names fails +expectError(propEq('foo', 'unknown', {} as Obj)); +// should work with arrays as well +expectType(propEq(1, 0, [] as NumArr)); +// numeric array should expect only numbers +expectError(propEq('foo', 0, [] as NumArr)); +// array can't accept string as prop name +expectError(propEq(1, 'foo', [] as NumArr)); +// ###################### // propEq(val)(name)(obj) expectType(propEq('foo')('union')({} as Obj)); // 'nope' is inferred as 'string' here. expectType(propEq('nope')('union')({} as Obj)); -// completely different type fails -expectError(propEq(2)('union')({} as Obj)); +// union of number with literal types should work fine +expectType(propEq(1)('numLike')({} as Obj)); +expectType(propEq('1')('numLike')({} as Obj)); +// optional types doesn't fire an error, if passed correct types +expectType(propEq('str')('optional')({} as Obj)); +expectType(propEq(undefined)('optional')({} as Obj)); +// fires an error only on wrong type +expectError(propEq(1)('optional')({} as Obj)); +expectError(propEq(null)('optional')({} as Obj)); +// nullable types doesn't fire an error, if passed correct types +expectType(propEq('str')('nullable')({} as Obj)); +expectType(propEq(null)('nullable')({} as Obj)); +// fires an error only on wrong type +expectError(propEq(1)('nullable')({} as Obj)); +expectError(propEq(undefined)('nullable')({} as Obj)); +// unknown field names fails +expectError(propEq('foo')('unknown')({} as Obj)); -// propEq(val)(name), obj) +// ###################### +// propEq(val)(name, obj) expectType(propEq('foo')('union', {} as Obj)); // 'nope' is inferred as 'string' here. expectType(propEq('nope')('union', {} as Obj)); -// completely different type fails -expectError(propEq(2)('union', {} as Obj)); +// union of number with literal types should work fine +expectType(propEq(1)('numLike', {} as Obj)); +expectType(propEq('1')('numLike', {} as Obj)); +// optional types doesn't fire an error, if passed correct types +expectType(propEq('str')('optional', {} as Obj)); +expectType(propEq(undefined)('optional', {} as Obj)); +// fires an error only on wrong type +expectError(propEq(1)('optional', {} as Obj)); +expectError(propEq(null)('optional', {} as Obj)); +// nullable types doesn't fire an error, if passed correct types +expectType(propEq('str')('nullable', {} as Obj)); +expectType(propEq(null)('nullable', {} as Obj)); +// fires an error only on wrong type +expectError(propEq(1)('nullable', {} as Obj)); +expectError(propEq(undefined)('nullable', {} as Obj)); +// unknown field names fails +expectError(propEq('foo')('unknown', {} as Obj)); +// ###################### // propEq(val, name)(obj) expectType(propEq('foo', 'union')({} as Obj)); // 'nope' is inferred as 'string' here. expectType(propEq('nope', 'union')({} as Obj)); -// completely different type fails -expectError(propEq(2, 'union')({} as Obj)); +// union of number with literal types should work fine +expectType(propEq(1, 'numLike')({} as Obj)); +expectType(propEq('1', 'numLike')({} as Obj)); +// optional types doesn't fire an error, if passed correct types +expectType(propEq('str', 'optional')({} as Obj)); +expectType(propEq(undefined, 'optional')({} as Obj)); +// fires an error only on wrong type +expectError(propEq(1, 'optional')({} as Obj)); +expectError(propEq(null, 'optional')({} as Obj)); +// nullable types doesn't fire an error, if passed correct types +expectType(propEq('str', 'nullable')({} as Obj)); +expectType(propEq(null, 'nullable')({} as Obj)); +// fires an error only on wrong type +expectError(propEq(1, 'nullable')({} as Obj)); +expectError(propEq(undefined, 'nullable')({} as Obj)); +// unknown field names fails +expectError(propEq('foo', 'unknown')({} as Obj)); + +// ########################## +// propEq(__, name, obj)(val) +expectType(propEq(__, 'union', {} as Obj)('foo')); +// propEq(val, __, obj)(val) +expectType(propEq('foo', __, {} as Obj)('union')); +// propEq(__, __, obj)(val, name) +expectType(propEq(__, __, {} as Obj)('foo', 'union')); +// propEq(__, __, obj)(val)(name) +expectType(propEq(__, __, {} as Obj)('foo')('union')); + +expectError(propEq('foo', __, {} as Obj)('unknown')); +expectError(propEq(__, __, {} as Obj)('foo', 'unknown')); +expectError(propEq(__, __, {} as Obj)('foo')('unknown')); diff --git a/types/propEq.d.ts b/types/propEq.d.ts index e667fb28..b5461f7c 100644 --- a/types/propEq.d.ts +++ b/types/propEq.d.ts @@ -1,6 +1,32 @@ -export function propEq(val: T): { - (name: K): (obj: Record) => boolean; - (name: K, obj: Record): boolean; +import { Placeholder } from 'ramda'; +import { WidenLiterals } from '../util/tools'; + +export function propEq(__: Placeholder): never; +export function propEq(val: V): { + (name: K): K extends number + ? (array: Array>) => boolean + : (obj: Record>) => boolean; + (__: Placeholder, obj: U): (name: keyof U) => boolean; + (name: K, obj: U): boolean; }; -export function propEq(val: T, name: K): (obj: Record) => boolean; -export function propEq(val: U[K], name: K, obj: U): boolean; +export function propEq(__: Placeholder, name: K): K extends number ? { + (__: Placeholder, array: U): (val: WidenLiterals) => boolean; + (val: WidenLiterals, array: U): boolean + (val: V): (array: Array>) => boolean +} : { + >(__: Placeholder, obj: U): (val: WidenLiterals) => boolean; + >(val: WidenLiterals, obj: U): boolean + (val: V): (obj: Record>) => boolean +}; +export function propEq(val: V, name: K): K extends number ? + (array: Array>) => boolean : + (obj: Record>) => boolean; + +export function propEq(__: Placeholder, ___: Placeholder, obj: U): { + (__: Placeholder, name: K): (val: WidenLiterals) => boolean + (val: WidenLiterals, name: K): boolean; + (val: WidenLiterals): (name: K) => boolean; +}; +export function propEq(__: Placeholder, name: K, obj: U): (val: WidenLiterals) => boolean; +export function propEq(val: WidenLiterals, __: Placeholder, obj: U): (name: K) => boolean; +export function propEq(val: WidenLiterals, name: K, obj: U): boolean;