diff --git a/src/wrapper.ts b/src/wrapper.ts index 1f002d4..e15af74 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -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 = {}) { @@ -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. diff --git a/tests/behaviors/guards.test.ts b/tests/behaviors/guards.test.ts new file mode 100644 index 0000000..d66a143 --- /dev/null +++ b/tests/behaviors/guards.test.ts @@ -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); + }); +});