And we want to remove the key from the storage, we have to pass undefined to setValueas we can see in this line. But this can't work with TS because TS will stay that it only accepts string or null.
If we try to set null to the value calling setValue(null) then the value will be "null" of type string but that's not what we wanted. We wanted the primitive null.
Then we have the option of typing useLocalstorageState<string | undefined>which then by your implementation returns the primitive null which is also what we don't want.
And I don't want to type my value as string | null | undefined because it's way too broad.
If we type something like this:
setValue
as we can see in this line. But this can't work with TS because TS will stay that it only acceptsstring
ornull
.null
to the value callingsetValue(null)
then the value will be"null"
of typestring
but that's not what we wanted. We wanted the primitivenull
.useLocalstorageState<string | undefined>
which then by your implementation returns the primitivenull
which is also what we don't want.And I don't want to type my value as
string | null | undefined
because it's way too broad.