Skip to content

Commit dea73c3

Browse files
committed
fix-assert-typing
1 parent 766c150 commit dea73c3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/debug.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { assert, timeit, tryCatch } from "./debug.ts";
88
import { assertEquals, assertThrows } from "std-assert";
99
import { sleep } from "./time.ts";
1010
import { throwerCatcherWithValue } from "./index.ts";
11+
import { pipe } from "./composition.ts";
1112

1213
Deno.test("timeit", () => {
1314
const logger = (x: string) => console.log(x);
@@ -53,6 +54,11 @@ Deno.test("assert", () => {
5354
assert(condition, err)(10);
5455
});
5556

57+
const _assertTyping: number = pipe(
58+
(x: number) => x,
59+
assert((x: number) => x > 3, "bla"),
60+
)(7);
61+
5662
Deno.test("assert async", async () => {
5763
const err = "not greater than 7";
5864
const condition = (x: number) => Promise.resolve(x > 7);

src/debug.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import type {
22
AsyncFunction,
33
Func,
44
IsAsync,
5+
ParamOf,
56
ReturnTypeUnwrapped,
67
} from "./typing.ts";
7-
import { pipe } from "./composition.ts";
8+
import { pipe, sideEffect } from "./composition.ts";
89
import { pairRight } from "./juxt.ts";
910
import { isPromise } from "./promise.ts";
1011
import { currentLocation } from "./trace.ts";
12+
import { letIn } from "./operator.ts";
1113

1214
export const sideLog = <T>(x: T) => {
1315
console.log(currentLocation(3), x);
@@ -84,13 +86,16 @@ export const timeit = <F extends Func>(
8486
return result;
8587
}) as F;
8688

87-
export const assert = <T>(
88-
condition: (_: T) => boolean | Promise<boolean>,
89+
export const assert = <F extends Func>(
90+
condition: F,
8991
errorMessage: string,
90-
) =>
92+
): true extends IsAsync<F> ? ((t: ParamOf<F>) => Promise<ParamOf<F>>)
93+
: ((t: ParamOf<F>) => ParamOf<F>) =>
94+
// @ts-expect-error not sure
9195
pipe(
96+
// @ts-expect-error not sure
9297
pairRight(condition),
93-
([value, passed]) => {
98+
([value, passed]: [ParamOf<F>, boolean]) => {
9499
if (!passed) throw new Error(errorMessage);
95100
return value;
96101
},

0 commit comments

Comments
 (0)