typed-typings / npm-ramda

TypeScript's type definitions for Ramda
MIT License
384 stars 64 forks source link

Adding support for type guards to `partition` function. #463

Open kaciakmaciak opened 1 year ago

kaciakmaciak commented 1 year ago

Hi, could you please add support for the type guards for the partition function.

Use case:

function isNumber(value: unknown): value is number {
  return typeof value === 'number' && !Number.isNaN(value);
}

const someList: Array<unknown> = ['bla', 'foo', 'bar', 1, 2, 3, undefined, null];

const [numbers, others] = R.partition(isNumber, someList);

Currently, both numbers and others arrays type is unknown[]. It would be helpful if the numbers type is number[].

We used the patch package to temporarily fix this:

diff --git [a/node_modules/@types/ramda/index.d.ts](mailto:a/node_modules/@types/ramda/index.d.ts) [b/node_modules/@types/ramda/index.d.ts](mailto:b/node_modules/@types/ramda/index.d.ts)
index 402e443..0a3040f 100755
--- [a/node_modules/@types/ramda/index.d.ts](mailto:a/node_modules/@types/ramda/index.d.ts)
+++ [b/node_modules/@types/ramda/index.d.ts](mailto:b/node_modules/@types/ramda/index.d.ts)
@@ -3733,7 +3733,9 @@ export function partialRight<T>(fn: (...args: any[]) => T, args: unknown[]): (..
  * ```
  */
export function partition(fn: (a: string) => boolean, list: readonly string[]): [string[], string[]];
+export function partition<T, P>(fn: (a: T) => a is P, list: readonly T[]): [P[], T[]];
export function partition<T>(fn: (a: T) => boolean, list: readonly T[]): [T[], T[]];
+export function partition<T, P>(fn: (a: T) => a is P): (list: readonly T[]) => [P[], T[]];
export function partition<T>(fn: (a: T) => boolean): (list: readonly T[]) => [T[], T[]];
export function partition(fn: (a: string) => boolean): (list: readonly string[]) => [string[], string[]];

Please let us know if you need any help in this. Can help.

Cheers, Katarina