]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(types/ref): update `MaybeRef` to include all ref-like types (#11379)
authorTycho <jh.leong@outlook.com>
Mon, 5 Aug 2024 02:40:17 +0000 (10:40 +0800)
committerGitHub <noreply@github.com>
Mon, 5 Aug 2024 02:40:17 +0000 (10:40 +0800)
Co-authored-by: Evan You <evan@vuejs.org>
packages/dts-test/ref.test-d.ts
packages/reactivity/src/ref.ts

index 0d3764fb7101a56999a1b3d7e2e22830ac26a74b..f8f2500aa1d0b639702451ea96e89031eba574ea 100644 (file)
@@ -5,6 +5,7 @@ import {
   type Ref,
   type ShallowRef,
   type ToRefs,
+  type WritableComputedRef,
   computed,
   isRef,
   proxyRefs,
@@ -465,8 +466,21 @@ describe('toRef <-> toValue', () => {
 })
 
 // unref
-declare const text: ShallowRef<string> | ComputedRef<string> | MaybeRef<string>
-expectType<string>(unref(text))
+// #8747
+declare const unref1: number | Ref<number> | ComputedRef<number>
+expectType<number>(unref(unref1))
+
+// #11356
+declare const unref2:
+  | MaybeRef<string>
+  | ShallowRef<string>
+  | ComputedRef<string>
+  | WritableComputedRef<string>
+expectType<string>(unref(unref2))
+
+// toValue
+expectType<number>(toValue(unref1))
+expectType<string>(toValue(unref2))
 
 // useTemplateRef
 const tRef = useTemplateRef('foo')
index ce6fded56f329d37e1e27b79ea1436d2f7a6ff8f..4cb2aa2c352cffed36eef0eb5139c8340f4f8cb1 100644 (file)
@@ -16,7 +16,7 @@ import {
   toRaw,
   toReactive,
 } from './reactive'
-import type { ComputedRef } from './computed'
+import type { ComputedRef, WritableComputedRef } from './computed'
 import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
 import { warn } from './warning'
 
@@ -192,8 +192,13 @@ export function triggerRef(ref: Ref) {
   }
 }
 
-export type MaybeRef<T = any> = T | Ref<T>
-export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
+export type MaybeRef<T = any> =
+  | T
+  | Ref<T>
+  | ShallowRef<T>
+  | WritableComputedRef<T>
+
+export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T)
 
 /**
  * Returns the inner value if the argument is a ref, otherwise return the
@@ -211,7 +216,7 @@ export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
  * @param ref - Ref or plain value to be converted into the plain value.
  * @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
  */
-export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<T>): T {
+export function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T {
   return isRef(ref) ? ref.value : ref
 }
 
@@ -231,9 +236,7 @@ export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<T>): T {
  * @param source - A getter, an existing ref, or a non-function value.
  * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
  */
-export function toValue<T>(
-  source: MaybeRefOrGetter<T> | ComputedRef<T> | ShallowRef<T>,
-): T {
+export function toValue<T>(source: MaybeRefOrGetter<T>): T {
   return isFunction(source) ? source() : unref(source)
 }