]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: typing for ref macros
authorEvan You <yyx990803@gmail.com>
Fri, 16 Jul 2021 21:35:04 +0000 (17:35 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 16 Jul 2021 21:35:04 +0000 (17:35 -0400)
packages/runtime-core/src/helpers/refMacros.ts [new file with mode: 0644]
packages/runtime-core/src/index.ts
packages/runtime-core/types/scriptSetupHelpers.d.ts
test-dts/refMacros.test-d.ts [new file with mode: 0644]

diff --git a/packages/runtime-core/src/helpers/refMacros.ts b/packages/runtime-core/src/helpers/refMacros.ts
new file mode 100644 (file)
index 0000000..4c62441
--- /dev/null
@@ -0,0 +1,21 @@
+import { Ref, UnwrapRef, ShallowUnwrapRef, ComputedRef } from '@vue/reactivity'
+
+export function $ref<T>(arg: T | Ref<T>): UnwrapRef<T>
+export function $ref() {}
+
+declare const ComputedRefMarker: unique symbol
+type ComputedRefValue<T> = T & { [ComputedRefMarker]?: any }
+
+export function $computed<T>(getter: () => T): ComputedRefValue<T>
+export function $computed() {}
+
+export function $fromRefs<T>(source: T): ShallowUnwrapRef<T>
+export function $fromRefs() {
+  return null as any
+}
+
+export function $raw<T>(value: ComputedRefValue<T>): ComputedRef<T>
+export function $raw<T>(value: T): Ref<T>
+export function $raw() {
+  return null as any
+}
index 3249f3a447dd021be75a784a2bfb26d89f187bbc..79784acc112c0f0490810804b49dfc5758278a48 100644 (file)
@@ -53,20 +53,19 @@ export { provide, inject } from './apiInject'
 export { nextTick } from './scheduler'
 export { defineComponent } from './apiDefineComponent'
 export { defineAsyncComponent } from './apiAsyncComponent'
+export { useAttrs, useSlots } from './apiSetupHelpers'
 
 // <script setup> API ----------------------------------------------------------
 
 export {
-  // macros runtime, for warnings only
+  // macros runtime, for typing and warnings only
   defineProps,
   defineEmits,
   defineExpose,
   withDefaults,
   // internal
   mergeDefaults,
-  withAsyncContext,
-  useAttrs,
-  useSlots
+  withAsyncContext
 } from './apiSetupHelpers'
 
 // Advanced API ----------------------------------------------------------------
@@ -345,3 +344,7 @@ const _compatUtils = {
 export const compatUtils = (__COMPAT__
   ? _compatUtils
   : null) as typeof _compatUtils
+
+// Ref macros ------------------------------------------------------------------
+// for dts generation only
+export { $ref, $computed, $raw, $fromRefs } from './helpers/refMacros'
index 4d168212c54392bb7aeb8d6d5e0c4512933c090d..fc56cb19cf446fcba1ac34742ac47a941ccb6aed 100644 (file)
@@ -5,9 +5,19 @@ type _defineEmits = typeof defineEmits
 type _defineExpose = typeof defineExpose
 type _withDefaults = typeof withDefaults
 
+type _ref = typeof $ref
+type _computed = typeof $computed
+type _fromRefs = typeof $fromRefs
+type _raw = typeof $raw
+
 declare global {
   const defineProps: _defineProps
   const defineEmits: _defineEmits
   const defineExpose: _defineExpose
   const withDefaults: _withDefaults
+
+  const $ref: _ref
+  const $computed: _computed
+  const $fromRefs: _fromRefs
+  const $raw: _raw
 }
diff --git a/test-dts/refMacros.test-d.ts b/test-dts/refMacros.test-d.ts
new file mode 100644 (file)
index 0000000..29d8370
--- /dev/null
@@ -0,0 +1,42 @@
+import {
+  expectType,
+  $ref,
+  $computed,
+  $fromRefs,
+  $raw,
+  ref,
+  Ref,
+  ComputedRef
+} from './index'
+
+// $ref
+expectType<number>($ref(1))
+expectType<number>($ref(ref(1)))
+expectType<{ foo: number }>($ref({ foo: ref(1) }))
+
+// $computed
+expectType<number>($computed(() => 1))
+let b = $ref(1)
+expectType<number>($computed(() => b))
+
+function useFoo() {
+  return {
+    x: ref(1),
+    y: ref('hi'),
+    z: 123
+  }
+}
+
+// $fromRefs
+const { x, y, z } = $fromRefs(useFoo())
+expectType<number>(x)
+expectType<string>(y)
+expectType<number>(z)
+
+// $raw
+expectType<Ref<number>>($raw(x))
+expectType<Ref<string>>($raw(y))
+
+const c = $computed(() => 1)
+const cRef = $raw(c)
+expectType<ComputedRef<number>>(cRef)