Skip to content

Commit

Permalink
refactor: no-any (#2471)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Mar 29, 2024
1 parent dbed0aa commit 42f324c
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 32 deletions.
6 changes: 4 additions & 2 deletions src/react/useAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ export function useAtom<Value>(
options?: Options,
): [Awaited<Value>, never]

export function useAtom<AtomType extends WritableAtom<any, any[], any>>(
export function useAtom<
AtomType extends WritableAtom<unknown, never[], unknown>,
>(
atom: AtomType,
options?: Options,
): [
Awaited<ExtractAtomValue<AtomType>>,
SetAtom<ExtractAtomArgs<AtomType>, ExtractAtomResult<AtomType>>,
]

export function useAtom<AtomType extends Atom<any>>(
export function useAtom<AtomType extends Atom<unknown>>(
atom: AtomType,
options?: Options,
): [Awaited<ExtractAtomValue<AtomType>>, never]
Expand Down
2 changes: 1 addition & 1 deletion src/react/useAtomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function useAtomValue<Value>(
options?: Options,
): Awaited<Value>

export function useAtomValue<AtomType extends Atom<any>>(
export function useAtomValue<AtomType extends Atom<unknown>>(
atom: AtomType,
options?: Options,
): Awaited<ExtractAtomValue<AtomType>>
Expand Down
4 changes: 3 additions & 1 deletion src/react/useSetAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function useSetAtom<Value, Args extends unknown[], Result>(
options?: Options,
): SetAtom<Args, Result>

export function useSetAtom<AtomType extends WritableAtom<any, any[], any>>(
export function useSetAtom<
AtomType extends WritableAtom<unknown, never[], unknown>,
>(
atom: AtomType,
options?: Options,
): SetAtom<ExtractAtomArgs<AtomType>, ExtractAtomResult<AtomType>>
Expand Down
6 changes: 3 additions & 3 deletions src/react/utils/useHydrateAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ type Store = ReturnType<typeof useStore>
type Options = Parameters<typeof useStore>[0] & {
dangerouslyForceHydrate?: boolean
}
type AnyWritableAtom = WritableAtom<unknown, any[], any>
type AnyWritableAtom = WritableAtom<unknown, never[], unknown>

type InferAtomTuples<T> = {
[K in keyof T]: T[K] extends readonly [infer A, unknown]
? A extends WritableAtom<unknown, infer Args, any>
? A extends WritableAtom<unknown, infer Args, infer _Result>
? readonly [A, Args[0]]
: T[K]
: never
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
8 changes: 6 additions & 2 deletions src/vanilla/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ export type ExtractAtomValue<AtomType> =
AtomType extends Atom<infer Value> ? Value : never

export type ExtractAtomArgs<AtomType> =
AtomType extends WritableAtom<any, infer Args, any> ? Args : never
AtomType extends WritableAtom<unknown, infer Args, infer _Result>
? Args
: never

export type ExtractAtomResult<AtomType> =
AtomType extends WritableAtom<any, any[], infer Result> ? Result : never
AtomType extends WritableAtom<unknown, infer _Args, infer Result>
? Result
: never

export type SetStateAction<Value> = ExtractAtomArgs<PrimitiveAtom<Value>>[0]
2 changes: 1 addition & 1 deletion src/vanilla/utils/atomWithReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function atomWithReducer<Value, Action>(
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))
})
}
2 changes: 1 addition & 1 deletion src/vanilla/utils/atomWithRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function atomWithRefresh<Value, Args extends unknown[], Result>(
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) {
Expand Down
12 changes: 6 additions & 6 deletions src/vanilla/utils/atomWithStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function withStorageValidator<Value>(
return validate(value)
},
}
return storage as any // FIXME better way to type this?
return storage
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export function createJSONStorage<Value>(
options?: JsonStorageOptions,
): AsyncStorage<Value> | SyncStorage<Value> {
let lastStr: string | undefined
let lastValue: any
let lastValue: Value
const storage: AsyncStorage<Value> | SyncStorage<Value> = {
getItem: (key, initialValue) => {
const parse = (str: string | null) => {
Expand All @@ -130,9 +130,9 @@ export function createJSONStorage<Value>(
}
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(
Expand Down Expand Up @@ -197,7 +197,7 @@ export function atomWithStorage<Value>(
| SyncStorage<Value>
| AsyncStorage<Value> = defaultStorage as SyncStorage<Value>,
options?: { getOnInit?: boolean },
): any {
) {
const getOnInit = options?.getOnInit
const baseAtom = atom(
getOnInit
Expand Down Expand Up @@ -244,5 +244,5 @@ export function atomWithStorage<Value>(
},
)

return anAtom
return anAtom as never
}
16 changes: 8 additions & 8 deletions src/vanilla/utils/freezeAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ const cache1 = new WeakMap()
const memo1 = <T>(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<AtomType extends Atom<any>>(
export function freezeAtom<AtomType extends Atom<unknown>>(
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<any>,
CreateAtom extends (...params: never[]) => Atom<unknown>,
>(createAtom: CreateAtom) {
return ((...params: any[]) => {
return ((...params: never[]) => {
const anAtom = createAtom(...params)
const origRead = anAtom.read
anAtom.read = function (get, options) {
Expand Down
10 changes: 5 additions & 5 deletions src/vanilla/utils/splitAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const isWritable = <Value, Args extends unknown[], Result>(
): atom is WritableAtom<Value, Args, Result> =>
!!(atom as WritableAtom<Value, Args, Result>).write

const isFunction = <T>(x: T): x is T & ((...args: any[]) => any) =>
const isFunction = <T>(x: T): x is T & ((...args: never[]) => unknown) =>
typeof x === 'function'

type SplitAtomAction<Item> =
Expand Down Expand Up @@ -94,7 +94,7 @@ export function splitAtom<Item, Key>(
}
throw new Error('splitAtom: index out of bounds for read')
}
return currArr[index] as Item
return currArr[index]!
}
const write = (
get: Getter,
Expand All @@ -109,7 +109,7 @@ export function splitAtom<Item, Key>(
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<Item[], [Item[]], void>, [
Expand Down Expand Up @@ -190,13 +190,13 @@ export function splitAtom<Item, Key>(
set(arrAtom as WritableAtom<Item[], [Item[]], void>, [
...arr.slice(0, index1),
...arr.slice(index1 + 1, index2),
arr[index1] as Item,
arr[index1]!,
...arr.slice(index2),
])
} else {
set(arrAtom as WritableAtom<Item[], [Item[]], void>, [
...arr.slice(0, index2),
arr[index1] as Item,
arr[index1]!,
...arr.slice(index2, index1),
...arr.slice(index1 + 1),
])
Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/utils/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function unwrap<Value, PendingValue>(

export function unwrap<Value, Args extends unknown[], Result, PendingValue>(
anAtom: WritableAtom<Value, Args, Result> | Atom<Value>,
fallback: (prev?: Awaited<Value>) => PendingValue = defaultFallback as any,
fallback: (prev?: Awaited<Value>) => PendingValue = defaultFallback as never,
) {
return memo2(
() => {
Expand Down

0 comments on commit 42f324c

Please sign in to comment.