Skip to content

Commit e0b2975

Browse files
authored
fix(types/ref): allow getter and setter types to be unrelated (#11442)
1 parent 8e052ee commit e0b2975

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

packages/dts-test/ref.test-d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ describe('ref with generic', <T extends { name: string }>() => {
172172
expectType<string>(ss.value.name)
173173
})
174174

175+
describe('allow getter and setter types to be unrelated', <T>() => {
176+
const a = { b: ref(0) }
177+
const c = ref(a)
178+
c.value = a
179+
180+
const d = {} as T
181+
const e = ref(d)
182+
e.value = d
183+
})
184+
175185
// shallowRef
176186
type Status = 'initial' | 'ready' | 'invalidating'
177187
const shallowStatus = shallowRef<Status>('initial')

packages/dts-test/watch.test-d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type ComputedRef,
3+
type MaybeRef,
34
type Ref,
45
computed,
56
defineComponent,
@@ -203,3 +204,10 @@ defineComponent({
203204
expectType<{ foo: string }>(value)
204205
})
205206
}
207+
208+
{
209+
const css: MaybeRef<string> = ''
210+
watch(ref(css), value => {
211+
expectType<string>(value)
212+
})
213+
}

packages/reactivity/src/ref.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import { warn } from './warning'
3030
declare const RefSymbol: unique symbol
3131
export declare const RawSymbol: unique symbol
3232

33-
export interface Ref<T = any> {
34-
value: T
33+
export interface Ref<T = any, S = T> {
34+
get value(): T
35+
set value(_: S)
3536
/**
3637
* Type differentiator only.
3738
* 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 {
108109
* @param value - The object to wrap in the ref.
109110
* @see {@link https://vuejs.org/api/reactivity-core.html#ref}
110111
*/
111-
export function ref<T>(value: T): Ref<UnwrapRef<T>>
112+
export function ref<T>(value: T): Ref<UnwrapRef<T>, UnwrapRef<T> | T>
112113
export function ref<T = any>(): Ref<T | undefined>
113114
export function ref(value?: unknown) {
114115
return createRef(value, false)

packages/runtime-core/src/apiWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { useSSRContext } from './helpers/useSsrContext'
4646

4747
export type WatchEffect = (onCleanup: OnCleanup) => void
4848

49-
export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
49+
export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T)
5050

5151
export type WatchCallback<V = any, OV = any> = (
5252
value: V,

0 commit comments

Comments
 (0)