Skip to content

Commit 4adc504

Browse files
committed
types: improve ref sugar $computed typing
1 parent 872b3f7 commit 4adc504

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

packages/runtime-core/src/helpers/refSugar.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Ref, UnwrapRef, ShallowUnwrapRef, ComputedRef } from '@vue/reactivity'
1+
import {
2+
Ref,
3+
UnwrapRef,
4+
ShallowUnwrapRef,
5+
ComputedRef,
6+
WritableComputedOptions,
7+
DebuggerOptions,
8+
WritableComputedRef
9+
} from '@vue/reactivity'
210

311
export function $ref<T>(arg: T | Ref<T>): UnwrapRef<T>
412
export function $ref() {}
@@ -8,17 +16,28 @@ export function $shallowRef<T>(arg: T): T {
816
}
917

1018
declare const ComputedRefMarker: unique symbol
11-
type ComputedRefValue<T> = T & { [ComputedRefMarker]?: any }
19+
type ComputedValue<T> = T & { [ComputedRefMarker]?: any }
1220

13-
export function $computed<T>(getter: () => T): ComputedRefValue<T>
21+
declare const WritableComputedRefMarker: unique symbol
22+
type WritableComputedValue<T> = T & { [WritableComputedRefMarker]?: any }
23+
24+
export function $computed<T>(
25+
getter: () => T,
26+
debuggerOptions?: DebuggerOptions
27+
): ComputedValue<T>
28+
export function $computed<T>(
29+
options: WritableComputedOptions<T>,
30+
debuggerOptions?: DebuggerOptions
31+
): WritableComputedValue<T>
1432
export function $computed() {}
1533

1634
export function $fromRefs<T>(source: T): ShallowUnwrapRef<T>
1735
export function $fromRefs() {
1836
return null as any
1937
}
2038

21-
export function $raw<T>(value: ComputedRefValue<T>): ComputedRef<T>
39+
export function $raw<T>(value: ComputedValue<T>): ComputedRef<T>
40+
export function $raw<T>(value: WritableComputedValue<T>): WritableComputedRef<T>
2241
export function $raw<T>(value: T): Ref<T>
2342
export function $raw() {
2443
return null as any

test-dts/refSugar.test-d.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WritableComputedRef } from '@vue/reactivity'
12
import {
23
expectType,
34
$ref,
@@ -22,7 +23,19 @@ expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
2223
// $computed
2324
expectType<number>($computed(() => 1))
2425
let b = $ref(1)
25-
expectType<number>($computed(() => b))
26+
expectType<number>(
27+
$computed(() => b, {
28+
onTrack() {}
29+
})
30+
)
31+
32+
// writable computed
33+
expectType<number>(
34+
$computed({
35+
get: () => 1,
36+
set: () => {}
37+
})
38+
)
2639

2740
function useFoo() {
2841
return {
@@ -45,3 +58,10 @@ expectType<Ref<string>>($raw(y))
4558
const c = $computed(() => 1)
4659
const cRef = $raw(c)
4760
expectType<ComputedRef<number>>(cRef)
61+
62+
const c2 = $computed({
63+
get: () => 1,
64+
set: () => {}
65+
})
66+
const c2Ref = $raw(c2)
67+
expectType<WritableComputedRef<number>>(c2Ref)

0 commit comments

Comments
 (0)