Skip to content

Commit b408451

Browse files
committed
feat(experimental): expose ref macro types using separate d.ts file
1 parent afd49b3 commit b408451

File tree

5 files changed

+97
-94
lines changed

5 files changed

+97
-94
lines changed

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

-50
This file was deleted.

packages/runtime-core/src/index.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ declare module '@vue/reactivity' {
149149
export {
150150
ReactiveEffectOptions,
151151
DebuggerEvent,
152+
DebuggerOptions,
152153
TrackOpTypes,
153154
TriggerOpTypes,
154155
Ref,
@@ -351,13 +352,3 @@ const _compatUtils = {
351352
export const compatUtils = (
352353
__COMPAT__ ? _compatUtils : null
353354
) as typeof _compatUtils
354-
355-
// Ref sugar macros ------------------------------------------------------------
356-
// for dts generation only
357-
export {
358-
$ref,
359-
$shallowRef,
360-
$computed,
361-
$raw,
362-
$fromRefs
363-
} from './helpers/refSugar'

packages/runtime-core/types/scriptSetupHelpers.d.ts

-12
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,9 @@ type _defineEmits = typeof defineEmits
55
type _defineExpose = typeof defineExpose
66
type _withDefaults = typeof withDefaults
77

8-
type _ref = typeof $ref
9-
type _shallowRef = typeof $shallowRef
10-
type _computed = typeof $computed
11-
type _fromRefs = typeof $fromRefs
12-
type _raw = typeof $raw
13-
148
declare global {
159
const defineProps: _defineProps
1610
const defineEmits: _defineEmits
1711
const defineExpose: _defineExpose
1812
const withDefaults: _withDefaults
19-
20-
const $ref: _ref
21-
const $shallowRef: _shallowRef
22-
const $computed: _computed
23-
const $fromRefs: _fromRefs
24-
const $raw: _raw
2513
}

packages/vue/ref-macros.d.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
Ref,
3+
UnwrapRef,
4+
ComputedRef,
5+
WritableComputedOptions,
6+
DebuggerOptions,
7+
WritableComputedRef,
8+
ShallowUnwrapRef
9+
} from '@vue/runtime-dom'
10+
11+
declare const RefMarker: unique symbol
12+
type RefValue<T> = T & { [RefMarker]?: any }
13+
14+
declare const ComputedRefMarker: unique symbol
15+
type ComputedRefValue<T> = T & { [ComputedRefMarker]?: any }
16+
17+
declare const WritableComputedRefMarker: unique symbol
18+
type WritableComputedRefValue<T> = T & { [WritableComputedRefMarker]?: any }
19+
20+
/**
21+
* Vue ref transform macro for binding refs as reactive variables.
22+
*/
23+
declare function _$<T>(arg: ComputedRef<T>): ComputedRefValue<T>
24+
declare function _$<T>(arg: WritableComputedRef<T>): WritableComputedRefValue<T>
25+
declare function _$<T>(arg: Ref<T>): RefValue<T>
26+
declare function _$<T extends object>(arg?: T): ShallowUnwrapRef<T>
27+
28+
/**
29+
* Vue ref transform macro for accessing underlying refs of reactive varaibles.
30+
*/
31+
declare function _$$<T>(value: T): ComputedRef<T>
32+
declare function _$$<T>(
33+
value: WritableComputedRefValue<T>
34+
): WritableComputedRef<T>
35+
declare function _$$<T>(value: RefValue<T>): Ref<T>
36+
declare function _$$<T extends object>(arg: T): ToRawRefs<T>
37+
38+
type ToRawRefs<T extends object> = {
39+
[K in keyof T]: T[K] extends ComputedRefValue<infer V>
40+
? ComputedRefValue<V>
41+
: T[K] extends WritableComputedRefValue<infer V>
42+
? WritableComputedRef<V>
43+
: T[K] extends RefValue<infer V>
44+
? Ref<V>
45+
: T[K] extends object
46+
? T[K] extends
47+
| Function
48+
| Map<any, any>
49+
| Set<any>
50+
| WeakMap<any, any>
51+
| WeakSet<any>
52+
? T[K]
53+
: ToRawRefs<T[K]>
54+
: T[K]
55+
}
56+
57+
declare function _$ref<T>(arg?: T | Ref<T>): RefValue<UnwrapRef<T>>
58+
59+
declare function _$shallowRef<T>(arg?: T): RefValue<T>
60+
61+
declare function _$computed<T>(
62+
getter: () => T,
63+
debuggerOptions?: DebuggerOptions
64+
): ComputedRefValue<T>
65+
declare function _$computed<T>(
66+
options: WritableComputedOptions<T>,
67+
debuggerOptions?: DebuggerOptions
68+
): WritableComputedRefValue<T>
69+
70+
declare global {
71+
const $: typeof _$
72+
const $$: typeof _$$
73+
const $ref: typeof _$ref
74+
const $shallowRef: typeof _$shallowRef
75+
const $computed: typeof _$computed
76+
}

test-dts/refSugar.test-d.ts renamed to test-dts/refTransformMacros.test-d.ts

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { WritableComputedRef } from '@vue/reactivity'
2-
import {
3-
expectType,
4-
$ref,
5-
$shallowRef,
6-
$computed,
7-
$fromRefs,
8-
$raw,
9-
ref,
10-
Ref,
11-
ComputedRef
12-
} from './index'
2+
import { expectType, ref, Ref, ComputedRef } from './index'
3+
import 'vue/ref-macros'
4+
5+
// wrapping refs
6+
// normal
7+
// computed
8+
// writable computed
9+
10+
// destructure
11+
const { x, y, z } = $(useFoo())
12+
expectType<number>(x)
13+
expectType<string>(y)
14+
expectType<number>(z)
1315

1416
// $ref
1517
expectType<number>($ref(1))
@@ -45,23 +47,19 @@ function useFoo() {
4547
}
4648
}
4749

48-
// $fromRefs
49-
const { x, y, z } = $fromRefs(useFoo())
50-
expectType<number>(x)
51-
expectType<string>(y)
52-
expectType<number>(z)
53-
54-
// $raw
55-
expectType<Ref<number>>($raw(x))
56-
expectType<Ref<string>>($raw(y))
50+
// $$
51+
expectType<Ref<number>>($$(x))
52+
expectType<Ref<string>>($$(y))
5753

5854
const c = $computed(() => 1)
59-
const cRef = $raw(c)
55+
const cRef = $$(c)
6056
expectType<ComputedRef<number>>(cRef)
6157

6258
const c2 = $computed({
6359
get: () => 1,
6460
set: () => {}
6561
})
66-
const c2Ref = $raw(c2)
62+
const c2Ref = $$(c2)
6763
expectType<WritableComputedRef<number>>(c2Ref)
64+
65+
// $$ on object

0 commit comments

Comments
 (0)