diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index 1456c523239..46d39214b93 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -172,6 +172,16 @@ describe('ref with generic', () => { expectType(ss.value.name) }) +describe('allow getter and setter types to be unrelated', () => { + const a = { b: ref(0) } + const c = ref(a) + c.value = a + + const d = {} as T + const e = ref(d) + e.value = d +}) + // shallowRef type Status = 'initial' | 'ready' | 'invalidating' const shallowStatus = shallowRef('initial') diff --git a/packages/dts-test/watch.test-d.ts b/packages/dts-test/watch.test-d.ts index 45c898ef672..6c8576f0c6b 100644 --- a/packages/dts-test/watch.test-d.ts +++ b/packages/dts-test/watch.test-d.ts @@ -1,5 +1,6 @@ import { type ComputedRef, + type MaybeRef, type Ref, computed, defineComponent, @@ -203,3 +204,10 @@ defineComponent({ expectType<{ foo: string }>(value) }) } + +{ + const css: MaybeRef = '' + watch(ref(css), value => { + expectType(value) + }) +} diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 3e9b05062f3..6e22d1bcd58 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -30,8 +30,9 @@ import { warn } from './warning' declare const RefSymbol: unique symbol export declare const RawSymbol: unique symbol -export interface Ref { - value: T +export interface Ref { + get value(): T + set value(_: S) /** * Type differentiator only. * 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. * @see {@link https://vuejs.org/api/reactivity-core.html#ref} */ -export function ref(value: T): Ref> +export function ref(value: T): Ref, UnwrapRef | T> export function ref(): Ref export function ref(value?: unknown) { return createRef(value, false) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index cdf8b8c888f..8b570bc28fa 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -46,7 +46,7 @@ import { useSSRContext } from './helpers/useSsrContext' export type WatchEffect = (onCleanup: OnCleanup) => void -export type WatchSource = Ref | ComputedRef | (() => T) +export type WatchSource = Ref | ComputedRef | (() => T) export type WatchCallback = ( value: V,