Skip to content

Commit

Permalink
improve guards
Browse files Browse the repository at this point in the history
  • Loading branch information
fcrozatier committed Jan 17, 2025
1 parent c38a015 commit a1d33ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export namespace Signal {
#brand() {}

static {
isState = (s) => #brand in s;
isState = (s) => typeof s === "object" && #brand in s;
}

constructor(initialValue: T, options: Signal.Options<T> = {}) {
Expand Down Expand Up @@ -84,7 +84,7 @@ export namespace Signal {
#brand() {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static {
isComputed = (c: any) => #brand in c;
isComputed = (c: any) => typeof c === "object" && #brand in c;
}

// Create a Signal which evaluates to the value returned by the callback.
Expand Down
22 changes: 22 additions & 0 deletions tests/behaviors/guards.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { Signal } from "../../src/wrapper.js";

describe("Guards", () => {
it("should work with Signals", () => {
const state = new Signal.State(1);
const computed = new Signal.Computed(() => state.get() * 2);
expect(Signal.isState(state)).toBe(true);
expect(Signal.isComputed(state)).toBe(false);

expect(Signal.isState(computed)).toBe(false);
expect(Signal.isComputed(computed)).toBe(true);
});

it("shouldn't error with values", () => {
expect(Signal.isState(1)).toBe(false);
expect(Signal.isComputed(2)).toBe(false);

expect(Signal.isState({})).toBe(false);
expect(Signal.isComputed({})).toBe(false);
});
});

0 comments on commit a1d33ab

Please sign in to comment.