You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While the ability to define the storage data type when using functions like storage.defineItem<Type>() is convenient, it's sadly not really type-safe - the contents of the storage can be modified either by code not using the same storage util, or even manually, so it's not guaranteed that we always get data of type Type when using getValue().
The idea is to allow passing a schema (preferably a zod schema) against which the value read from the storage would be validated internally in getValue() before being returned. A nice bonus is that it would be easy to automatically infer the type based on the schema, so the dev wouldn't have to specify it on top of the schema.
It could look sth like this:
constmyStorage=storage.defineItem('local:my-storage',z.object({foo: z.number()}))myStorage.getValue().then(value=>/* fully type-safe `{ foo: number }` */)
and the types for defineItem() would go sth like this:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
While the ability to define the storage data type when using functions like
storage.defineItem<Type>()
is convenient, it's sadly not really type-safe - the contents of the storage can be modified either by code not using the same storage util, or even manually, so it's not guaranteed that we always get data of typeType
when usinggetValue()
.The idea is to allow passing a schema (preferably a zod schema) against which the value read from the storage would be validated internally in
getValue()
before being returned. A nice bonus is that it would be easy to automatically infer the type based on the schema, so the dev wouldn't have to specify it on top of the schema.It could look sth like this:
and the types for
defineItem()
would go sth like this:Alternatively, if we wanted to avoid vendor-lockin on zod, we could just have a
validate
parameter:Beta Was this translation helpful? Give feedback.
All reactions