Skip to content

TS problems with WritableComputedRef and generic types #641

@leopiccionia

Description

@leopiccionia

Version

3.0.0-alpha.2

Reproduction link

https://www.typescriptlang.org/play/index.html#code/JYWwDg9gTgLgBAbzgYwuArjApgEwDRxRYBmcAvnMVGnAOQACAbulgPRECGyMwjwMAT1oAoYcXQA7bsAgSUnbACUSACmAT wDgBsAajpYAuOBPQgARligBKRMLgOUsgM7wAHnAC8hVes079bRZre0dUCVc4AS8nDGwcFQRQx0cAcywYYxVbTwA ODcAOkYDLDxklLhnDKySoKxjUwsrHPykyo6C4tKYupYKlLIKshCUohh0KDkkNwJooaGxSWlZeSwOJRIAJgAeABVctQ0eANLjPdt2sJd3GKJiI-89UtHriPho71Q43ESBtJqcGyXnyRT6ZX DmqmSB4POrTsnUqYJ63nBkPIw1eDnGk2mBTmmMWwmQCiwygeABYtiFSetNsRdk1LFBDtTrEA

Steps to reproduce

import { computed, ref } from '@vue/reactivity'

function createRef(initialValue: number) {
    const x = ref(initialValue)
    const y = computed({
        get: () => x.value,
        set: (value: number) => {
            x.value = value
        }
    })
    return { x, y }
}

function createRef2<T>(initialValue: T) {
    const x = ref(initialValue)
    const y = computed({
        get: () => x.value,
        set: (value: T) => {
            x.value = value
        }
    })
    return { x, y }
}

createRef(42)
createRef2(42)

What is expected?

I expect createRef2 to work without issues, just like createRef, with y inside createRef2 having type WritableComputedRef<T>.

What is actually happening?

TypeScript can't deduce type of y inside createRef2.


It may be just a misunderstanding of new Vue API, but seemed like a bug to me at first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions