Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zipObj): stricter types, add test #88

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/zipObj.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expectType } from 'tsd';
import { zipObj } from '../types/zipObj';
import { pipe } from '../types/pipe';

const sym = Symbol.for('Symbol');

// zipObj(key, values)
expectType<{bool: true, number: 1, null: null, undefined: undefined}>(zipObj(['bool', 'number', 'null', 'undefined'], [true, 1, null, undefined]));
expectType<{1: 1, [sym]: symbol}>(zipObj([1, sym], [1, sym]));
expectType<{bool: boolean, number: number}>(zipObj(['bool', 'number'], [true, 1] as [boolean, number]));
expectType<{[key: string]: boolean}>(zipObj(['a'] as string[], [true, false] as boolean[]));
expectType<{a: boolean}>(zipObj(['a'], [true, false] as boolean[]));
expectType<{[key: string]: boolean}>(zipObj(['a'] as string[], [true, false]));
Harris-Miller marked this conversation as resolved.
Show resolved Hide resolved
expectType<{[key: string]: boolean | number | string}>(zipObj(['a', 'b', 'c'] as string[], [true, 1, 'a'] as (string | number | boolean)[]));
expectType<{bool: true, number: 1}>(zipObj(['bool', 'number'], [true, 1, 'string']));
expectType<{bool: true, number: 1, missing: undefined}>(zipObj(['bool', 'number', 'missing'], [true, 1]));

// zipObj(key)(values)
expectType<{bool: true, number: 1, null: null, undefined: undefined}>(zipObj(['bool', 'number', 'null', 'undefined'])([true, 1, null, undefined]));
expectType<{1: 1, [sym]: symbol}>(zipObj([1, sym])([1, sym]));
expectType<{bool: boolean, number: number}>(zipObj(['bool', 'number'])([true, 1] as [boolean, number]));
expectType<{[key: string]: boolean}>(zipObj(['a'] as string[])([true, false] as boolean[]));
expectType<{a: boolean}>(zipObj(['a'])([true, false] as boolean[]));
expectType<{[key: string]: boolean}>(zipObj(['a'] as string[])([true, false]));
expectType<{bool: true, number: 1}>(zipObj(['bool', 'number'])([true, 1, 'string']));
expectType<{bool: true, number: 1, missing: undefined}>(zipObj(['bool', 'number', 'missing'])([true, 1]));


expectType<{string: string, number: number}>(pipe(
(a: [string, number]) => a,
zipObj(['string', 'number'])
)(['a', 42]));
7 changes: 7 additions & 0 deletions types/util/zipObj.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as _ from 'ts-toolbelt';

export type _ZipObj<K extends readonly PropertyKey[], V extends {[key in keyof K]: unknown} | readonly unknown[]> =
number extends V['length'] ?
{ [T in K[number]]: V[number] } :
number extends K['length'] ?
{ [T in K[number]]: V[number] } : _.L.ZipObj<K, V>;
10 changes: 8 additions & 2 deletions types/zipObj.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function zipObj<K extends PropertyKey>(keys: readonly K[]): <T>(values: readonly T[]) => { [P in K]: T };
export function zipObj<T, K extends PropertyKey>(keys: readonly K[], values: readonly T[]): { [P in K]: T };
import * as _ from 'ts-toolbelt';
import { _ZipObj } from './util/zipObj';

export function zipObj<K extends readonly PropertyKey[]>(keys: _.F.Narrow<K>):
<V extends {[key in keyof K]: unknown} | readonly unknown[]>(values: _.F.Narrow<V>) =>
_ZipObj<K, V>;

export function zipObj<K extends readonly PropertyKey[], V extends {[key in keyof K]: unknown} | readonly unknown[]>(keys: _.F.Narrow<K>, values: _.F.Narrow<V>):
_ZipObj<K, V>;
Loading