Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // implementation
- class TypedStorage {
- constructor({ prefix }) {
- }
- addSchema(key, schema, fallback: (parsed) => typeof schema) {
- }
- // invalidate if value doesn't pass validation
- getItem(key) {
- }
- setItem(key, fallback) {
- }
- }
- // usage
- const UserSchema = zod.object({ name: zod.string().required(), age: zod.number() })
- const typedStorage = new TypedStorage({ prefix: 'myStore' })
- // key, schema, fallback
- .addSchema('users', zod.array(UserSchema), () => [])
- const users: typeof UserSchema = [
- {
- name: 'asdasd',
- age: 12,
- }
- ]
- typedStorage.setItem('users', users)
- const parsedUsers = typedStorage.getItem('users') // typeof UserSchema[]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement