diff --git a/packages/runtime-core/src/helpers/refSugar.ts b/packages/runtime-core/src/helpers/refSugar.ts index 91480dfa..b9d8d7c9 100644 --- a/packages/runtime-core/src/helpers/refSugar.ts +++ b/packages/runtime-core/src/helpers/refSugar.ts @@ -1,4 +1,12 @@ -import { Ref, UnwrapRef, ShallowUnwrapRef, ComputedRef } from '@vue/reactivity' +import { + Ref, + UnwrapRef, + ShallowUnwrapRef, + ComputedRef, + WritableComputedOptions, + DebuggerOptions, + WritableComputedRef +} from '@vue/reactivity' export function $ref(arg: T | Ref): UnwrapRef export function $ref() {} @@ -8,9 +16,19 @@ export function $shallowRef(arg: T): T { } declare const ComputedRefMarker: unique symbol -type ComputedRefValue = T & { [ComputedRefMarker]?: any } +type ComputedValue = T & { [ComputedRefMarker]?: any } -export function $computed(getter: () => T): ComputedRefValue +declare const WritableComputedRefMarker: unique symbol +type WritableComputedValue = T & { [WritableComputedRefMarker]?: any } + +export function $computed( + getter: () => T, + debuggerOptions?: DebuggerOptions +): ComputedValue +export function $computed( + options: WritableComputedOptions, + debuggerOptions?: DebuggerOptions +): WritableComputedValue export function $computed() {} export function $fromRefs(source: T): ShallowUnwrapRef @@ -18,7 +36,8 @@ export function $fromRefs() { return null as any } -export function $raw(value: ComputedRefValue): ComputedRef +export function $raw(value: ComputedValue): ComputedRef +export function $raw(value: WritableComputedValue): WritableComputedRef export function $raw(value: T): Ref export function $raw() { return null as any diff --git a/test-dts/refSugar.test-d.ts b/test-dts/refSugar.test-d.ts index 4ea579b9..3f8c670d 100644 --- a/test-dts/refSugar.test-d.ts +++ b/test-dts/refSugar.test-d.ts @@ -1,3 +1,4 @@ +import { WritableComputedRef } from '@vue/reactivity' import { expectType, $ref, @@ -22,7 +23,19 @@ expectType<{ foo: Ref }>($shallowRef({ foo: ref(1) })) // $computed expectType($computed(() => 1)) let b = $ref(1) -expectType($computed(() => b)) +expectType( + $computed(() => b, { + onTrack() {} + }) +) + +// writable computed +expectType( + $computed({ + get: () => 1, + set: () => {} + }) +) function useFoo() { return { @@ -45,3 +58,10 @@ expectType>($raw(y)) const c = $computed(() => 1) const cRef = $raw(c) expectType>(cRef) + +const c2 = $computed({ + get: () => 1, + set: () => {} +}) +const c2Ref = $raw(c2) +expectType>(c2Ref)