Просмотр исходного кода

fix(types/ref): correct type inference for nested refs (#11536)

close #11532
close #11537
Tycho 1 год назад
Родитель
Сommit
536f62332c
2 измененных файлов с 8 добавлено и 1 удалено
  1. 5 0
      packages/dts-test/ref.test-d.ts
  2. 3 1
      packages/reactivity/src/ref.ts

+ 5 - 0
packages/dts-test/ref.test-d.ts

@@ -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

+ 3 - 1
packages/reactivity/src/ref.ts

@@ -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)