-
I want to use a generic setter in my custom hook. However, typescript doesn't like my generic setter import {Accessor, createSignal} from "solid-js"
type Result<T> = [isLoading: Accessor<boolean>, getData: Accessor<T>, getError: Accessor<Error>]
export function useWebService<T>(service: Promise<T>, defaultValue: T): Result<T> {
const [isLoading, setLoading] = createSignal(true)
const [getData, setData] = createSignal<T>(defaultValue)
const [getError, setError] = createSignal<Error>(null)
service
.then(data => setData(data)) // TS2345: Argument of type 'T' is not assignable to parameter of type 'Exclude | ((prev: T) => T)'
.catch(error => setError(error))
.finally(() => setLoading(true))
return [isLoading, getData, getError]
} I found an ugly workaround by using |
Beta Was this translation helpful? Give feedback.
Answered by
pmwmedia
May 12, 2022
Replies: 2 comments
-
I just found a clean solution. I have to use import {Accessor, createSignal} from "solid-js"
type Result<T> = [isLoading: Accessor<boolean>, getData: Accessor<T>, getError: Accessor<Error>]
export function useWebService<T>(service: Promise<Exclude<T, Function>>, defaultValue: Exclude<T, Function>): Result<T> {
const [isLoading, setLoading] = createSignal(true)
const [getData, setData] = createSignal<T>(defaultValue)
const [getError, setError] = createSignal<Error>(null)
service
.then(data => setData(data))
.catch(error => setError(error))
.finally(() => setLoading(true))
return [isLoading, getData, getError]
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pmwmedia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just found a clean solution. I have to use
Exclude<T, Function>
: