Skip to content

Commit

Permalink
spread-retains-output-type
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Aug 13, 2023
1 parent 7629de2 commit fd3f2e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/operator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { between, modulo, prop } from "./operator.ts";
import { between, modulo, prop, spread } from "./operator.ts";

import { assertEquals } from "https://deno.land/std@0.174.0/testing/asserts.ts";

Expand All @@ -21,3 +21,10 @@ const assertString = (x: string) => x;
prop<{ a: number }>()("a")({ a: 1 }) as number;
// @ts-expect-error: type does not match
assertString(prop<{ a: number }>()("a")({ a: 1 }));

spread((x: string, y: number) => x + y)(["a", 1]);
// @ts-expect-error retains input type
spread((x: string, y: number) => x + y)(["a", "a"]);
const _1: string = spread((x: string, y: number) => x + y)(["a", 1]);
// @ts-expect-error retains output type
const _2: number = spread((x: string, y: number) => x + y)(["a", 1]);
6 changes: 4 additions & 2 deletions src/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const greaterEquals = (x: number) => (y: number) => y >= x;
export const smallerEquals = (x: number) => (y: number) => y <= x;
export const between = (start: number, end: number) => (x: number) =>
start <= x && x < end;
export const unspread = <Inputs extends unknown[]>(...stuff: Inputs): Inputs =>
// deno-lint-ignore no-explicit-any
export const unspread = <Inputs extends any[]>(...stuff: Inputs): Inputs =>
stuff;
export const spread = <F extends Func>(f: F) => (x: Parameters<F>) => f(...x);
export const spread = <F extends Func>(f: F) => (x: Parameters<F>) =>
f(...x) as ReturnType<F>;
export const modulo = (y: number) => (x: number) => x % y;

0 comments on commit fd3f2e5

Please sign in to comment.