Skip to content

Commit 327c898

Browse files
committed
types: typing for ref macros
1 parent 1bab53e commit 327c898

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Ref, UnwrapRef, ShallowUnwrapRef, ComputedRef } from '@vue/reactivity'
2+
3+
export function $ref<T>(arg: T | Ref<T>): UnwrapRef<T>
4+
export function $ref() {}
5+
6+
declare const ComputedRefMarker: unique symbol
7+
type ComputedRefValue<T> = T & { [ComputedRefMarker]?: any }
8+
9+
export function $computed<T>(getter: () => T): ComputedRefValue<T>
10+
export function $computed() {}
11+
12+
export function $fromRefs<T>(source: T): ShallowUnwrapRef<T>
13+
export function $fromRefs() {
14+
return null as any
15+
}
16+
17+
export function $raw<T>(value: ComputedRefValue<T>): ComputedRef<T>
18+
export function $raw<T>(value: T): Ref<T>
19+
export function $raw() {
20+
return null as any
21+
}

packages/runtime-core/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,19 @@ export { provide, inject } from './apiInject'
5353
export { nextTick } from './scheduler'
5454
export { defineComponent } from './apiDefineComponent'
5555
export { defineAsyncComponent } from './apiAsyncComponent'
56+
export { useAttrs, useSlots } from './apiSetupHelpers'
5657

5758
// <script setup> API ----------------------------------------------------------
5859

5960
export {
60-
// macros runtime, for warnings only
61+
// macros runtime, for typing and warnings only
6162
defineProps,
6263
defineEmits,
6364
defineExpose,
6465
withDefaults,
6566
// internal
6667
mergeDefaults,
67-
withAsyncContext,
68-
useAttrs,
69-
useSlots
68+
withAsyncContext
7069
} from './apiSetupHelpers'
7170

7271
// Advanced API ----------------------------------------------------------------
@@ -345,3 +344,7 @@ const _compatUtils = {
345344
export const compatUtils = (__COMPAT__
346345
? _compatUtils
347346
: null) as typeof _compatUtils
347+
348+
// Ref macros ------------------------------------------------------------------
349+
// for dts generation only
350+
export { $ref, $computed, $raw, $fromRefs } from './helpers/refMacros'

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ type _defineEmits = typeof defineEmits
55
type _defineExpose = typeof defineExpose
66
type _withDefaults = typeof withDefaults
77

8+
type _ref = typeof $ref
9+
type _computed = typeof $computed
10+
type _fromRefs = typeof $fromRefs
11+
type _raw = typeof $raw
12+
813
declare global {
914
const defineProps: _defineProps
1015
const defineEmits: _defineEmits
1116
const defineExpose: _defineExpose
1217
const withDefaults: _withDefaults
18+
19+
const $ref: _ref
20+
const $computed: _computed
21+
const $fromRefs: _fromRefs
22+
const $raw: _raw
1323
}

test-dts/refMacros.test-d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
expectType,
3+
$ref,
4+
$computed,
5+
$fromRefs,
6+
$raw,
7+
ref,
8+
Ref,
9+
ComputedRef
10+
} from './index'
11+
12+
// $ref
13+
expectType<number>($ref(1))
14+
expectType<number>($ref(ref(1)))
15+
expectType<{ foo: number }>($ref({ foo: ref(1) }))
16+
17+
// $computed
18+
expectType<number>($computed(() => 1))
19+
let b = $ref(1)
20+
expectType<number>($computed(() => b))
21+
22+
function useFoo() {
23+
return {
24+
x: ref(1),
25+
y: ref('hi'),
26+
z: 123
27+
}
28+
}
29+
30+
// $fromRefs
31+
const { x, y, z } = $fromRefs(useFoo())
32+
expectType<number>(x)
33+
expectType<string>(y)
34+
expectType<number>(z)
35+
36+
// $raw
37+
expectType<Ref<number>>($raw(x))
38+
expectType<Ref<string>>($raw(y))
39+
40+
const c = $computed(() => 1)
41+
const cRef = $raw(c)
42+
expectType<ComputedRef<number>>(cRef)

0 commit comments

Comments
 (0)