luisherranz / deepsignal

DeepSignal 🧶 - Preact signals, but using regular JavaScript objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shouldn't type DeepSignalObject be exported?

oravecz opened this issue · comments

I'm attempting to create a store using deepsignal in TypeScript.

I'm having a problem when I attempt to type my store, and in particular my Store's state object. Don't I need to declare state as a type of DeepSignalObject? But that type is not exported from the library.

interface AuthState {
  auth: boolean;
}

interface AuthActions {
  isAuthenticated: () => boolean;
  setIsAuthenticated: (isAuthenticated: boolean) => void;
  signOut: () => void;
}

interface DeepAuthState {
  state: DeepSignalObject<AuthState>;
}

type AuthStore = DeepAuthState & AuthActions;

export const authStore: AuthStore = {
  state: deepSignal<AuthState>({
    auth: false,
  }),

  isAuthenticated() {
    return this.state.auth;
  },

  setIsAuthenticated(isAuthenticated: boolean): void {
    this.state.auth.value = isAuthenticated;
  },

  signOut(): void {
    setIsAuthenticated(false);
  },
};

Just use the DeepSignal type:

import { deepSignal, DeepSignal } from "deepsignal";

interface AuthState {
  auth: boolean;
}

interface AuthActions {
  isAuthenticated: () => boolean;
  setIsAuthenticated: (isAuthenticated: boolean) => void;
  signOut: () => void;
}

interface DeepAuthState {
  state: DeepSignal<AuthState>; // Use the DeepSignal type
}

type AuthStore = DeepAuthState & AuthActions;

export const authStore: AuthStore = {
  state: deepSignal<AuthState>({
    auth: false,
  }),

  isAuthenticated() {
    return this.state.auth;
  },

  setIsAuthenticated(isAuthenticated: boolean): void {
    this.state.auth = isAuthenticated; // Don't use .value here, just mutate the object prop
  },

  signOut(): void {
    this.setIsAuthenticated(false);
  },
};

Thanks for that!

No problem 🙂

I've improved the docs to explain how DeepSignal works: bf1bae2