]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix shallowRef return type with union value type (#7853)
authorSimon Lévesque <simlevesque@dismail.de>
Fri, 10 Nov 2023 05:40:47 +0000 (00:40 -0500)
committerGitHub <noreply@github.com>
Fri, 10 Nov 2023 05:40:47 +0000 (13:40 +0800)
close #7852

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

index bbcde45ddda05793726f39e82262c7e996074430..542d9d6a9efcb12ae66597899e283eb2fec129ca 100644 (file)
@@ -15,9 +15,10 @@ import {
   MaybeRef,
   MaybeRefOrGetter,
   ComputedRef,
-  computed
+  computed,
+  ShallowRef
 } from 'vue'
-import { expectType, describe } from './utils'
+import { expectType, describe, IsUnion } from './utils'
 
 function plainType(arg: number | Ref<number>) {
   // ref coercing
@@ -174,6 +175,27 @@ if (refStatus.value === 'initial') {
   refStatus.value = 'invalidating'
 }
 
+{
+  const shallow = shallowRef(1)
+  expectType<Ref<number>>(shallow)
+  expectType<ShallowRef<number>>(shallow)
+}
+
+{
+  //#7852
+  type Steps = { step: '1' } | { step: '2' }
+  const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
+  const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
+
+  expectType<IsUnion<typeof shallowUnionGenParam>>(false)
+  expectType<IsUnion<typeof shallowUnionAsCast>>(false)
+}
+
+describe('shallowRef with generic', <T>() => {
+  const r = ref({}) as MaybeRef<T>
+  expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
+})
+
 // proxyRefs: should return `reactive` directly
 const r1 = reactive({
   k: 'v'
index 9d727999bccc033a76f2978dd968005fddd03ac0..cc631d37acbda0540e2abbbf0ae4b8d17c0d2cea 100644 (file)
@@ -1,4 +1,4 @@
-import { ref, computed, watch, defineComponent } from 'vue'
+import { ref, computed, watch, defineComponent, shallowRef } from 'vue'
 import { expectType } from './utils'
 
 const source = ref('foo')
@@ -92,3 +92,17 @@ defineComponent({
     )
   }
 })
+
+{
+  //#7852
+  type Steps = { step: '1' } | { step: '2' }
+  const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
+  const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
+
+  watch(shallowUnionGenParam, value => {
+    expectType<Steps>(value)
+  })
+  watch(shallowUnionAsCast, value => {
+    expectType<Steps>(value)
+  })
+}
index 915f5760878ebf0c6fef53d2f59ad79d71ad5850..68844310e644d222e7991adc6c837886942c16c4 100644 (file)
@@ -115,9 +115,8 @@ export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }
  * @param value - The "inner value" for the shallow ref.
  * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
  */
-export function shallowRef<T extends object>(
-  value: T
-): T extends Ref ? T : ShallowRef<T>
+export function shallowRef<T>(value: MaybeRef<T>): Ref<T> | ShallowRef<T>
+export function shallowRef<T extends Ref>(value: T): T
 export function shallowRef<T>(value: T): ShallowRef<T>
 export function shallowRef<T = any>(): ShallowRef<T | undefined>
 export function shallowRef(value?: unknown) {