Преглед изворни кода

fix(types): ref() return type should not be any when initial value is any (#9768)

丶远方 пре 2 година
родитељ
комит
cdac12161e
2 измењених фајлова са 11 додато и 2 уклоњено
  1. 11 1
      packages/dts-test/ref.test-d.ts
  2. 0 1
      packages/reactivity/src/ref.ts

+ 11 - 1
packages/dts-test/ref.test-d.ts

@@ -18,7 +18,7 @@ import {
   computed,
   computed,
   ShallowRef
   ShallowRef
 } from 'vue'
 } from 'vue'
-import { expectType, describe, IsUnion } from './utils'
+import { expectType, describe, IsUnion, IsAny } from './utils'
 
 
 function plainType(arg: number | Ref<number>) {
 function plainType(arg: number | Ref<number>) {
   // ref coercing
   // ref coercing
@@ -79,6 +79,10 @@ function plainType(arg: number | Ref<number>) {
   // should still unwrap in objects nested in arrays
   // should still unwrap in objects nested in arrays
   const arr2 = ref([{ a: ref(1) }]).value
   const arr2 = ref([{ a: ref(1) }]).value
   expectType<number>(arr2[0].a)
   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)
 plainType(1)
@@ -191,6 +195,12 @@ if (refStatus.value === 'initial') {
   expectType<IsUnion<typeof shallowUnionAsCast>>(false)
   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>() => {
 describe('shallowRef with generic', <T>() => {
   const r = ref({}) as MaybeRef<T>
   const r = ref({}) as MaybeRef<T>
   expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
   expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))

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

@@ -87,7 +87,6 @@ export function isRef(r: any): r is Ref {
  * @param value - The object to wrap in the ref.
  * @param value - The object to wrap in the ref.
  * @see {@link https://vuejs.org/api/reactivity-core.html#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>(value: T): Ref<UnwrapRef<T>>
 export function ref<T = any>(): Ref<T | undefined>
 export function ref<T = any>(): Ref<T | undefined>
 export function ref(value?: unknown) {
 export function ref(value?: unknown) {