Skip to content

Generic setter for createSignal() is rejected by Typescript transpiler #980

Answered by pmwmedia
pmwmedia asked this question in Q&A
Discussion options

You must be logged in to vote

I just found a clean solution. I have to use Exclude<T, Function>:

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]
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by pmwmedia
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants