mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

Handling generic typing for D.toPairs() #82

Open SamCB-CT opened 1 year ago

SamCB-CT commented 1 year ago

Based on the current definition:

function toPairs<T, K extends string | number>(dict: Record<K, T>): Array<readonly [string, T]>

The first element in each pair is a string but it should use the generic type K. For example:

type Keys = 'foo' | 'bar' | 'baz';

const input: Record<Keys, number> = {foo: 42, bar: 0, baz: -1};
const output = D.toPairs(input);

The definition for output in the .d.ts becomes:

declare const output: (readonly [string, number])[];

But I'd expect it to be:

declare const output: (readonly [Keys, number])[];

Typescript Playground Example

By updating the definition to:

function toPairs<T, K extends string | number>(dict: Record<K, T>): Array<readonly [K, T]>;

This seems to fix it (see playground) but I'm not enough across how the gentype rescript stuff works to be able to make a PR.