#Setter type errors

6 messages · Page 1 of 1 (latest)

drowsy mica
#

I'm trying to create a custom signal to use in the storage option of a resource
The run-time logic of the setter works just fine but there's a type error that I can't pinpoint

north latch
#

Have you tried with as const?

return [getter, setter] as const
north latch
drowsy mica
#

i'm pretty sure the type error is on the setter itself, not on the signal return

the resource storage option is not throwing any errors

I should've included a text version of the issue though

type StateSongs = { name: string; rating: number }[];

export const storeBackedSongs: InitializedResourceOptions<
  StateSongs,
  true
>["storage"] = () => {
  const [state, setState] = useGlobal();

  const getter: Accessor<StateSongs> = () => state.songs;

  const setter: Setter<StateSongs | undefined> = (value) => {
    const songs = unwrap(state.songs);
    if (typeof value === "function") {
      const newSongs = value(songs);
      if (newSongs) setState("songs", newSongs);
      return newSongs;
    }
    if (value) setState("songs", value);
    return value;
  };

  return [getter, setter];
};
#

and the error formatted nicely

type One = <U extends StateSongs | undefined>
  | (value: ((prev: StateSongs | undefined) => U)
  | Exclude<U, Function>
  | ((prev: StateSongs | undefined) => U)) => U;

// not assignable to
type Two = Setter<StateSongs | undefined>

//Target signature provides too few arguments. Expected 1 or more, but got 0.
north latch
#

Well, it's not very clear how it works in the source code either. Where I can only suggest that we make it so ¯_(ツ)_/¯