Skip to content

Commit

Permalink
fix(types): fix withStorageValidator type (#2410)
Browse files Browse the repository at this point in the history
* add failing test

* fix it
  • Loading branch information
dai-shi authored Feb 21, 2024
1 parent a9eeadb commit c6f1af5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/vanilla/utils/atomWithStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ export interface SyncStringStorage {

export function withStorageValidator<Value>(
validator: (value: unknown) => value is Value,
): (storage: AsyncStorage<unknown>) => AsyncStorage<Value>

export function withStorageValidator<Value>(
validator: (value: unknown) => value is Value,
): (storage: SyncStorage<unknown>) => SyncStorage<Value>
): {
(storage: AsyncStorage<any>): AsyncStorage<Value>
(storage: SyncStorage<any>): SyncStorage<Value>
}

export function withStorageValidator<Value>(
validator: (value: unknown) => value is Value,
Expand Down
15 changes: 14 additions & 1 deletion tests/react/vanilla-utils/atomWithStorage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { act, fireEvent, render, waitFor } from '@testing-library/react'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { useAtom } from 'jotai/react'
import { atom, createStore } from 'jotai/vanilla'
import { RESET, atomWithStorage, createJSONStorage } from 'jotai/vanilla/utils'
import {
RESET,
atomWithStorage,
createJSONStorage,
unstable_withStorageValidator as withStorageValidator,
} from 'jotai/vanilla/utils'

const resolve: (() => void)[] = []

Expand Down Expand Up @@ -588,3 +593,11 @@ describe('atomWithStorage (with non-browser storage)', () => {
)
})
})

describe('withStorageValidator', () => {
it('should use withStorageValidator with isNumber', () => {
const storage = createJSONStorage<number>()
const isNumber = (v: unknown): v is number => typeof v === 'number'
atomWithStorage('my-number', 0, withStorageValidator(isNumber)(storage))
})
})

0 comments on commit c6f1af5

Please sign in to comment.