From 42f324c3b7a4e71181074540015c3068aee05153 Mon Sep 17 00:00:00 2001 From: Daishi Kato Date: Fri, 29 Mar 2024 17:56:26 +0900 Subject: [PATCH] refactor: no-any (#2471) --- src/react/useAtom.ts | 6 ++++-- src/react/useAtomValue.ts | 2 +- src/react/useSetAtom.ts | 4 +++- src/react/utils/useHydrateAtoms.ts | 6 +++--- src/vanilla/store.ts | 2 +- src/vanilla/typeUtils.ts | 8 ++++++-- src/vanilla/utils/atomWithReducer.ts | 2 +- src/vanilla/utils/atomWithRefresh.ts | 2 +- src/vanilla/utils/atomWithStorage.ts | 12 ++++++------ src/vanilla/utils/freezeAtom.ts | 16 ++++++++-------- src/vanilla/utils/splitAtom.ts | 10 +++++----- src/vanilla/utils/unwrap.ts | 2 +- 12 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/react/useAtom.ts b/src/react/useAtom.ts index a504a079c8..29784db288 100644 --- a/src/react/useAtom.ts +++ b/src/react/useAtom.ts @@ -29,7 +29,9 @@ export function useAtom( options?: Options, ): [Awaited, never] -export function useAtom>( +export function useAtom< + AtomType extends WritableAtom, +>( atom: AtomType, options?: Options, ): [ @@ -37,7 +39,7 @@ export function useAtom>( SetAtom, ExtractAtomResult>, ] -export function useAtom>( +export function useAtom>( atom: AtomType, options?: Options, ): [Awaited>, never] diff --git a/src/react/useAtomValue.ts b/src/react/useAtomValue.ts index 0e3baa1297..8c28b62ebd 100644 --- a/src/react/useAtomValue.ts +++ b/src/react/useAtomValue.ts @@ -50,7 +50,7 @@ export function useAtomValue( options?: Options, ): Awaited -export function useAtomValue>( +export function useAtomValue>( atom: AtomType, options?: Options, ): Awaited> diff --git a/src/react/useSetAtom.ts b/src/react/useSetAtom.ts index 47a20747cd..167fa068a4 100644 --- a/src/react/useSetAtom.ts +++ b/src/react/useSetAtom.ts @@ -14,7 +14,9 @@ export function useSetAtom( options?: Options, ): SetAtom -export function useSetAtom>( +export function useSetAtom< + AtomType extends WritableAtom, +>( atom: AtomType, options?: Options, ): SetAtom, ExtractAtomResult> diff --git a/src/react/utils/useHydrateAtoms.ts b/src/react/utils/useHydrateAtoms.ts index a703caa759..dad27cfc28 100644 --- a/src/react/utils/useHydrateAtoms.ts +++ b/src/react/utils/useHydrateAtoms.ts @@ -5,11 +5,11 @@ type Store = ReturnType type Options = Parameters[0] & { dangerouslyForceHydrate?: boolean } -type AnyWritableAtom = WritableAtom +type AnyWritableAtom = WritableAtom type InferAtomTuples = { [K in keyof T]: T[K] extends readonly [infer A, unknown] - ? A extends WritableAtom + ? A extends WritableAtom ? readonly [A, Args[0]] : T[K] : never @@ -43,7 +43,7 @@ export function useHydrateAtoms< for (const [atom, value] of values) { if (!hydratedSet.has(atom) || options?.dangerouslyForceHydrate) { hydratedSet.add(atom) - store.set(atom, value) + store.set(atom, value as never) } } } diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 432bf485b6..36fa46cf22 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -459,7 +459,7 @@ export const createStore = () => { }, } try { - const valueOrPromise = atom.read(getter, options as any) + const valueOrPromise = atom.read(getter, options as never) return setAtomValueOrPromise(atom, valueOrPromise, nextDependencies, () => controller?.abort(), ) diff --git a/src/vanilla/typeUtils.ts b/src/vanilla/typeUtils.ts index 96344e3925..dbec0c1887 100644 --- a/src/vanilla/typeUtils.ts +++ b/src/vanilla/typeUtils.ts @@ -9,9 +9,13 @@ export type ExtractAtomValue = AtomType extends Atom ? Value : never export type ExtractAtomArgs = - AtomType extends WritableAtom ? Args : never + AtomType extends WritableAtom + ? Args + : never export type ExtractAtomResult = - AtomType extends WritableAtom ? Result : never + AtomType extends WritableAtom + ? Result + : never export type SetStateAction = ExtractAtomArgs>[0] diff --git a/src/vanilla/utils/atomWithReducer.ts b/src/vanilla/utils/atomWithReducer.ts index 5799fb2f2a..d7c1ee478f 100644 --- a/src/vanilla/utils/atomWithReducer.ts +++ b/src/vanilla/utils/atomWithReducer.ts @@ -15,7 +15,7 @@ export function atomWithReducer( initialValue: Value, reducer: (value: Value, action: Action) => Value, ) { - return atom(initialValue, function (this: any, get, set, action: Action) { + return atom(initialValue, function (this: never, get, set, action: Action) { set(this, reducer(get(this), action)) }) } diff --git a/src/vanilla/utils/atomWithRefresh.ts b/src/vanilla/utils/atomWithRefresh.ts index 679d7208b7..626f1a4034 100644 --- a/src/vanilla/utils/atomWithRefresh.ts +++ b/src/vanilla/utils/atomWithRefresh.ts @@ -32,7 +32,7 @@ export function atomWithRefresh( return atom( (get, options) => { get(refreshAtom) - return read(get, options as any) + return read(get, options as never) }, (get, set, ...args: Args) => { if (args.length === 0) { diff --git a/src/vanilla/utils/atomWithStorage.ts b/src/vanilla/utils/atomWithStorage.ts index 7b044f41dc..038c76feaf 100644 --- a/src/vanilla/utils/atomWithStorage.ts +++ b/src/vanilla/utils/atomWithStorage.ts @@ -73,7 +73,7 @@ export function withStorageValidator( return validate(value) }, } - return storage as any // FIXME better way to type this? + return storage } } @@ -113,7 +113,7 @@ export function createJSONStorage( options?: JsonStorageOptions, ): AsyncStorage | SyncStorage { let lastStr: string | undefined - let lastValue: any + let lastValue: Value const storage: AsyncStorage | SyncStorage = { getItem: (key, initialValue) => { const parse = (str: string | null) => { @@ -130,9 +130,9 @@ export function createJSONStorage( } const str = getStringStorage()?.getItem(key) ?? null if (isPromiseLike(str)) { - return str.then(parse) + return str.then(parse) as never } - return parse(str) + return parse(str) as never }, setItem: (key, newValue) => getStringStorage()?.setItem( @@ -197,7 +197,7 @@ export function atomWithStorage( | SyncStorage | AsyncStorage = defaultStorage as SyncStorage, options?: { getOnInit?: boolean }, -): any { +) { const getOnInit = options?.getOnInit const baseAtom = atom( getOnInit @@ -244,5 +244,5 @@ export function atomWithStorage( }, ) - return anAtom + return anAtom as never } diff --git a/src/vanilla/utils/freezeAtom.ts b/src/vanilla/utils/freezeAtom.ts index b5c0d05001..6918045760 100644 --- a/src/vanilla/utils/freezeAtom.ts +++ b/src/vanilla/utils/freezeAtom.ts @@ -5,33 +5,33 @@ const cache1 = new WeakMap() const memo1 = (create: () => T, dep1: object): T => (cache1.has(dep1) ? cache1 : cache1.set(dep1, create())).get(dep1) -const deepFreeze = (obj: any) => { +const deepFreeze = (obj: unknown) => { if (typeof obj !== 'object' || obj === null) return Object.freeze(obj) const propNames = Object.getOwnPropertyNames(obj) for (const name of propNames) { - const value = obj[name] + const value = (obj as never)[name] deepFreeze(value) } return obj } -export function freezeAtom>( +export function freezeAtom>( anAtom: AtomType, ): AtomType { return memo1(() => { - const frozenAtom: any = atom( + const frozenAtom = atom( (get) => deepFreeze(get(anAtom)), - (_get, set, arg) => set(anAtom as any, arg), + (_get, set, arg) => set(anAtom as never, arg), ) - return frozenAtom + return frozenAtom as never }, anAtom) } export function freezeAtomCreator< - CreateAtom extends (...params: any[]) => Atom, + CreateAtom extends (...params: never[]) => Atom, >(createAtom: CreateAtom) { - return ((...params: any[]) => { + return ((...params: never[]) => { const anAtom = createAtom(...params) const origRead = anAtom.read anAtom.read = function (get, options) { diff --git a/src/vanilla/utils/splitAtom.ts b/src/vanilla/utils/splitAtom.ts index 69bcca4c89..7eec2d4ff3 100644 --- a/src/vanilla/utils/splitAtom.ts +++ b/src/vanilla/utils/splitAtom.ts @@ -22,7 +22,7 @@ const isWritable = ( ): atom is WritableAtom => !!(atom as WritableAtom).write -const isFunction = (x: T): x is T & ((...args: any[]) => any) => +const isFunction = (x: T): x is T & ((...args: never[]) => unknown) => typeof x === 'function' type SplitAtomAction = @@ -94,7 +94,7 @@ export function splitAtom( } throw new Error('splitAtom: index out of bounds for read') } - return currArr[index] as Item + return currArr[index]! } const write = ( get: Getter, @@ -109,7 +109,7 @@ export function splitAtom( throw new Error('splitAtom: index out of bounds for write') } const nextItem = isFunction(update) - ? update(arr[index] as Item) + ? (update as (prev: Item) => Item)(arr[index]!) : update if (!Object.is(arr[index], nextItem)) { set(arrAtom as WritableAtom, [ @@ -190,13 +190,13 @@ export function splitAtom( set(arrAtom as WritableAtom, [ ...arr.slice(0, index1), ...arr.slice(index1 + 1, index2), - arr[index1] as Item, + arr[index1]!, ...arr.slice(index2), ]) } else { set(arrAtom as WritableAtom, [ ...arr.slice(0, index2), - arr[index1] as Item, + arr[index1]!, ...arr.slice(index2, index1), ...arr.slice(index1 + 1), ]) diff --git a/src/vanilla/utils/unwrap.ts b/src/vanilla/utils/unwrap.ts index c83e4a6f50..fe6269636d 100644 --- a/src/vanilla/utils/unwrap.ts +++ b/src/vanilla/utils/unwrap.ts @@ -39,7 +39,7 @@ export function unwrap( export function unwrap( anAtom: WritableAtom | Atom, - fallback: (prev?: Awaited) => PendingValue = defaultFallback as any, + fallback: (prev?: Awaited) => PendingValue = defaultFallback as never, ) { return memo2( () => {