]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): ref() return type should not be any when initial value is any (#9768)
author丶远方 <yangpanteng@gmail.com>
Thu, 7 Dec 2023 08:28:16 +0000 (16:28 +0800)
committerGitHub <noreply@github.com>
Thu, 7 Dec 2023 08:28:16 +0000 (16:28 +0800)
packages/dts-test/ref.test-d.ts
packages/reactivity/src/ref.ts

index 542d9d6a9efcb12ae66597899e283eb2fec129ca..a467c446d0a39d66d49dfefc94e653f007f48546 100644 (file)
@@ -18,7 +18,7 @@ import {
   computed,
   ShallowRef
 } from 'vue'
-import { expectType, describe, IsUnion } from './utils'
+import { expectType, describe, IsUnion, IsAny } from './utils'
 
 function plainType(arg: number | Ref<number>) {
   // ref coercing
@@ -79,6 +79,10 @@ function plainType(arg: number | Ref<number>) {
   // should still unwrap in objects nested in arrays
   const arr2 = ref([{ a: ref(1) }]).value
   expectType<number>(arr2[0].a)
+
+  // any value should return Ref<any>, not any
+  const a = ref(1 as any)
+  expectType<IsAny<typeof a>>(false)
 }
 
 plainType(1)
@@ -191,6 +195,12 @@ if (refStatus.value === 'initial') {
   expectType<IsUnion<typeof shallowUnionAsCast>>(false)
 }
 
+{
+  // any value should return Ref<any>, not any
+  const a = shallowRef(1 as any)
+  expectType<IsAny<typeof a>>(false)
+}
+
 describe('shallowRef with generic', <T>() => {
   const r = ref({}) as MaybeRef<T>
   expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
index 9f7ebdad8222c2ec0d2de5e395cc084dfd16f5ca..f068844100e86bcfbcfe492e46e846c8ae0b7540 100644 (file)
@@ -87,7 +87,6 @@ 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 extends Ref>(value: T): T
 export function ref<T>(value: T): Ref<UnwrapRef<T>>
 export function ref<T = any>(): Ref<T | undefined>
 export function ref(value?: unknown) {