]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types/ref): correct type inference for nested refs (#11536)
authorTycho <jh.leong@outlook.com>
Wed, 7 Aug 2024 03:20:04 +0000 (11:20 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Aug 2024 03:20:04 +0000 (11:20 +0800)
close #11532
close #11537

packages/dts-test/ref.test-d.ts
packages/reactivity/src/ref.ts

index 46d39214b930758449c9c310073d65debea0e937..3161d9d8b55714bc372aa79494589c2e66e56c65 100644 (file)
@@ -180,6 +180,11 @@ describe('allow getter and setter types to be unrelated', <T>() => {
   const d = {} as T
   const e = ref(d)
   e.value = d
+
+  const f = ref(ref(0))
+  expectType<number>(f.value)
+  // @ts-expect-error
+  f.value = ref(1)
 })
 
 // shallowRef
index 6e22d1bcd58ca29a9fd10f3966db6542166a30ca..3128a8413c0ea024aeaa5349ff55156e7b5637c4 100644 (file)
@@ -109,7 +109,9 @@ export function isRef(r: any): r is Ref {
  * @param value - The object to wrap in the ref.
  * @see {@link https://vuejs.org/api/reactivity-core.html#ref}
  */
-export function ref<T>(value: T): Ref<UnwrapRef<T>, UnwrapRef<T> | T>
+export function ref<T>(
+  value: T,
+): [T] extends [Ref] ? IfAny<T, Ref<T>, T> : Ref<UnwrapRef<T>, UnwrapRef<T> | T>
 export function ref<T = any>(): Ref<T | undefined>
 export function ref(value?: unknown) {
   return createRef(value, false)