Skip to content

Commit c3adec8

Browse files
authored
improve guards (#45)
* improve guards * run prettier
1 parent c38a015 commit c3adec8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/wrapper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export namespace Signal {
4444
#brand() {}
4545

4646
static {
47-
isState = (s) => #brand in s;
47+
isState = (s) => typeof s === 'object' && #brand in s;
4848
}
4949

5050
constructor(initialValue: T, options: Signal.Options<T> = {}) {
@@ -84,7 +84,7 @@ export namespace Signal {
8484
#brand() {}
8585
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8686
static {
87-
isComputed = (c: any) => #brand in c;
87+
isComputed = (c: any) => typeof c === 'object' && #brand in c;
8888
}
8989

9090
// Create a Signal which evaluates to the value returned by the callback.

tests/behaviors/guards.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {describe, expect, it} from 'vitest';
2+
import {Signal} from '../../src/wrapper.js';
3+
4+
describe('Guards', () => {
5+
it('should work with Signals', () => {
6+
const state = new Signal.State(1);
7+
const computed = new Signal.Computed(() => state.get() * 2);
8+
expect(Signal.isState(state)).toBe(true);
9+
expect(Signal.isComputed(state)).toBe(false);
10+
11+
expect(Signal.isState(computed)).toBe(false);
12+
expect(Signal.isComputed(computed)).toBe(true);
13+
});
14+
15+
it("shouldn't error with values", () => {
16+
expect(Signal.isState(1)).toBe(false);
17+
expect(Signal.isComputed(2)).toBe(false);
18+
19+
expect(Signal.isState({})).toBe(false);
20+
expect(Signal.isComputed({})).toBe(false);
21+
});
22+
});

0 commit comments

Comments
 (0)