From dea73c3f7e29cbc14614e163bc6aacb628d6eb23 Mon Sep 17 00:00:00 2001 From: uri Date: Fri, 10 Jan 2025 21:04:53 +0200 Subject: [PATCH] fix-assert-typing --- src/debug.test.ts | 6 ++++++ src/debug.ts | 15 ++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/debug.test.ts b/src/debug.test.ts index cad97dc..d86aff9 100644 --- a/src/debug.test.ts +++ b/src/debug.test.ts @@ -8,6 +8,7 @@ import { assert, timeit, tryCatch } from "./debug.ts"; import { assertEquals, assertThrows } from "std-assert"; import { sleep } from "./time.ts"; import { throwerCatcherWithValue } from "./index.ts"; +import { pipe } from "./composition.ts"; Deno.test("timeit", () => { const logger = (x: string) => console.log(x); @@ -53,6 +54,11 @@ Deno.test("assert", () => { assert(condition, err)(10); }); +const _assertTyping: number = pipe( + (x: number) => x, + assert((x: number) => x > 3, "bla"), +)(7); + Deno.test("assert async", async () => { const err = "not greater than 7"; const condition = (x: number) => Promise.resolve(x > 7); diff --git a/src/debug.ts b/src/debug.ts index 3658fe6..723d578 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -2,12 +2,14 @@ import type { AsyncFunction, Func, IsAsync, + ParamOf, ReturnTypeUnwrapped, } from "./typing.ts"; -import { pipe } from "./composition.ts"; +import { pipe, sideEffect } from "./composition.ts"; import { pairRight } from "./juxt.ts"; import { isPromise } from "./promise.ts"; import { currentLocation } from "./trace.ts"; +import { letIn } from "./operator.ts"; export const sideLog = (x: T) => { console.log(currentLocation(3), x); @@ -84,13 +86,16 @@ export const timeit = ( return result; }) as F; -export const assert = ( - condition: (_: T) => boolean | Promise, +export const assert = ( + condition: F, errorMessage: string, -) => +): true extends IsAsync ? ((t: ParamOf) => Promise>) + : ((t: ParamOf) => ParamOf) => + // @ts-expect-error not sure pipe( + // @ts-expect-error not sure pairRight(condition), - ([value, passed]) => { + ([value, passed]: [ParamOf, boolean]) => { if (!passed) throw new Error(errorMessage); return value; },