Skip to content

Commit

Permalink
fix(propEq): improve propEq typings
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nemo108 committed Oct 14, 2023
1 parent 1a36036 commit 70ca60d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion test/anyPass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ expectType<boolean>(
})
);

expectError(
expectType<boolean>(
isVampire({
age: 21,
garlic_allergy: true,
Expand Down
35 changes: 24 additions & 11 deletions test/propEq.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectError, expectType } from 'tsd';

import { propEq } from '../es';
import {__, propEq} from '../es';

type Obj = {
union: 'foo' | 'bar';
Expand All @@ -12,28 +12,41 @@ type Obj = {

// propEq(val, name, obj)
expectType<boolean>(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<boolean>(propEq(1, 'union', {} as Obj));
// unknown field names fails
expectError(propEq('foo', 'unknown', {} as Obj));

// propEq(val)(name)(obj)
expectType<boolean>(propEq('foo')('union')({} as Obj));
// 'nope' is inferred as 'string' here.
expectType<boolean>(propEq('nope')('union')({} as Obj));
// completely different type fails
expectError(propEq(2)('union')({} as Obj));
// unknown field names fails
expectError(propEq('foo')('unknown')({} as Obj));

// propEq(val)(name), obj)
expectType<boolean>(propEq('foo')('union', {} as Obj));
// 'nope' is inferred as 'string' here.
expectType<boolean>(propEq('nope')('union', {} as Obj));
// completely different type fails
expectError(propEq(2)('union', {} as Obj));
// unknown field names fails
expectError(propEq('foo')('unknown', {} as Obj));

// propEq(val, name)(obj)
expectType<boolean>(propEq('foo', 'union')({} as Obj));
// 'nope' is inferred as 'string' here.
expectType<boolean>(propEq('nope', 'union')({} as Obj));
// completely different type fails
expectError(propEq(2, 'union')({} as Obj));
// unknown field names fails
expectError(propEq('foo', 'unknown')({} as Obj));

// propEq(__, name, obj)(val)
expectType<boolean>(propEq(__, 'union', {} as Obj)('foo'));
// propEq(val, __, obj)(val)
expectType<boolean>(propEq('foo', __, {} as Obj)('union'));
// propEq(__, __, obj)(val, name)
expectType<boolean>(propEq(__, __, {} as Obj)('foo', 'union'));
// propEq(__, __, obj)(val)(name)
expectType<boolean>(propEq(__, __, {} as Obj)('foo')('union'));

expectError(propEq('foo', __, {} as Obj)('unknown'));
expectError(propEq(__, __, {} as Obj)('foo', 'unknown'));
expectError(propEq(__, __, {} as Obj)('foo')('unknown'));
27 changes: 22 additions & 5 deletions types/propEq.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
export function propEq<T>(val: T): {
<K extends PropertyKey>(name: K): (obj: Record<K, T>) => boolean;
<K extends PropertyKey>(name: K, obj: Record<K, T>): boolean;
import { Placeholder } from 'ramda';

export function propEq(__: Placeholder): never;
export function propEq<V>(val: V): {
<K extends PropertyKey>(name: K): (obj: Record<K, any>) => boolean;
<U extends Record<PropertyKey, any>>(__: Placeholder, obj: U): (name: keyof U) => boolean;
<K extends keyof U, U extends Record<PropertyKey, any>>(name: K, obj: U): boolean;
};
export function propEq<T, K extends PropertyKey>(val: T, name: K): (obj: Record<K, T>) => boolean;
export function propEq<K extends keyof U, U>(val: U[K], name: K, obj: U): boolean;
export function propEq<K extends PropertyKey>(__: Placeholder, name: K): {
(__: Placeholder, obj: Record<K, any>): <V>(val: V) => boolean;
<V>(val: V, obj: Record<K, any>): boolean
<V>(val: V): (obj: Record<K, any>) => boolean
};
export function propEq<V, K extends PropertyKey>(val: V, name: K): (obj: Record<K, any>) => boolean;


export function propEq<U extends Record<any, any>>(__: Placeholder, ___: Placeholder, obj: U): {
<V extends Exclude<any, Placeholder>>(val: V, name: keyof U): boolean;
<V extends Exclude<any, Placeholder>>(val: V): (name: keyof U) => boolean;
};
export function propEq<K extends PropertyKey>(__: Placeholder, name: K, obj: Record<K, any>): <V>(val: V) => boolean;
export function propEq<V, U extends Record<any, any>>(val: V, __: Placeholder, obj: U): (name: keyof U) => boolean;
export function propEq<U extends Record<any, any>>(val: Exclude<any, Placeholder>, name: keyof U, obj: U): boolean;

0 comments on commit 70ca60d

Please sign in to comment.