S
SolidJS14mo ago
andi

Setter type errors

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
No description
4 Replies
Driver
Driver14mo ago
Have you tried with as const?
return [getter, setter] as const
return [getter, setter] as const
Yes, I wrote a silly thing, it had nothing to do with that.
andi
andiOP14mo ago
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];
};
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.
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.
Driver
Driver14mo ago
Well, it's not very clear how it works in the source code either. Where I can only suggest that we make it so ¯\_(ツ)_/¯
No description
No description
Driver
Driver14mo ago
No description
Want results from more Discord servers?
Add your server