Fixed useFetch and useUser typing

This commit is contained in:
2023-07-20 00:55:12 +03:00
parent 7a044970f0
commit bc154f8b6b
3 changed files with 16 additions and 22 deletions

View File

@ -3,40 +3,31 @@ import { useEffect, useState } from 'react'
import { SetState } from '../utils/types'
import useSend from './useSend'
type UseFetchShared = {
type UseFetchShared<T> = {
loading: boolean,
abort?: () => void,
setData: SetState<T | undefined>
}
type UseFetchSucced<T> = {
error: null,
data: T,
} & UseFetchShared
} & UseFetchShared<T>
type UseFetchErrored = {
type UseFetchErrored<T> = {
error: string,
data: undefined
} & UseFetchShared
} & UseFetchShared<T>
const gotError = <T>(res: UseFetchErrored | UseFetchSucced<T>): res is UseFetchErrored => (
const gotError = <T>(res: UseFetchErrored<T> | UseFetchSucced<T>): res is UseFetchErrored<T> => (
typeof res.error === 'string'
)
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored) => (
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored<T>) => (
gotError(res) ? res.error : res.data
)
type UseFetchReturn<T> = ({
error: null,
data: T
} | {
error: string,
data: undefined
}) & {
loading: boolean,
setData: SetState<T | undefined>
abort?: (() => void)
}
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored<T>
function useFetch<R, T>(
url: string,
@ -70,7 +61,7 @@ function useFetch<R, T>(
}
}
export type { UseFetchErrored, UseFetchSucced }
export type { UseFetchReturn }
export default useFetch