fix(types/ref): allow getter and setter types to be unrelated (#11442)

This commit is contained in:
Tycho 2024-07-29 10:52:38 +08:00 committed by GitHub
parent 8e052eecf3
commit e0b2975ef6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 4 deletions

View File

@ -172,6 +172,16 @@ describe('ref with generic', <T extends { name: string }>() => {
expectType<string>(ss.value.name) expectType<string>(ss.value.name)
}) })
describe('allow getter and setter types to be unrelated', <T>() => {
const a = { b: ref(0) }
const c = ref(a)
c.value = a
const d = {} as T
const e = ref(d)
e.value = d
})
// shallowRef // shallowRef
type Status = 'initial' | 'ready' | 'invalidating' type Status = 'initial' | 'ready' | 'invalidating'
const shallowStatus = shallowRef<Status>('initial') const shallowStatus = shallowRef<Status>('initial')

View File

@ -1,5 +1,6 @@
import { import {
type ComputedRef, type ComputedRef,
type MaybeRef,
type Ref, type Ref,
computed, computed,
defineComponent, defineComponent,
@ -203,3 +204,10 @@ defineComponent({
expectType<{ foo: string }>(value) expectType<{ foo: string }>(value)
}) })
} }
{
const css: MaybeRef<string> = ''
watch(ref(css), value => {
expectType<string>(value)
})
}

View File

@ -30,8 +30,9 @@ import { warn } from './warning'
declare const RefSymbol: unique symbol declare const RefSymbol: unique symbol
export declare const RawSymbol: unique symbol export declare const RawSymbol: unique symbol
export interface Ref<T = any> { export interface Ref<T = any, S = T> {
value: T get value(): T
set value(_: S)
/** /**
* Type differentiator only. * Type differentiator only.
* We need this to be in public d.ts but don't want it to show up in IDE * We need this to be in public d.ts but don't want it to show up in IDE
@ -108,7 +109,7 @@ export function isRef(r: any): r is Ref {
* @param value - The object to wrap in the ref. * @param value - The object to wrap in the ref.
* @see {@link https://vuejs.org/api/reactivity-core.html#ref} * @see {@link https://vuejs.org/api/reactivity-core.html#ref}
*/ */
export function ref<T>(value: T): Ref<UnwrapRef<T>> export function ref<T>(value: T): Ref<UnwrapRef<T>, UnwrapRef<T> | T>
export function ref<T = any>(): Ref<T | undefined> export function ref<T = any>(): Ref<T | undefined>
export function ref(value?: unknown) { export function ref(value?: unknown) {
return createRef(value, false) return createRef(value, false)

View File

@ -46,7 +46,7 @@ import { useSSRContext } from './helpers/useSsrContext'
export type WatchEffect = (onCleanup: OnCleanup) => void export type WatchEffect = (onCleanup: OnCleanup) => void
export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T) export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T)
export type WatchCallback<V = any, OV = any> = ( export type WatchCallback<V = any, OV = any> = (
value: V, value: V,