From: 丶远方 Date: Thu, 7 Dec 2023 08:28:16 +0000 (+0800) Subject: fix(types): ref() return type should not be any when initial value is any (#9768) X-Git-Tag: v3.3.11~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cdac12161ec27b45ded48854c3d749664b6d4a6d;p=thirdparty%2Fvuejs%2Fcore.git fix(types): ref() return type should not be any when initial value is any (#9768) --- diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index 542d9d6a9e..a467c446d0 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -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) { // ref coercing @@ -79,6 +79,10 @@ function plainType(arg: number | Ref) { // should still unwrap in objects nested in arrays const arr2 = ref([{ a: ref(1) }]).value expectType(arr2[0].a) + + // any value should return Ref, not any + const a = ref(1 as any) + expectType>(false) } plainType(1) @@ -191,6 +195,12 @@ if (refStatus.value === 'initial') { expectType>(false) } +{ + // any value should return Ref, not any + const a = shallowRef(1 as any) + expectType>(false) +} + describe('shallowRef with generic', () => { const r = ref({}) as MaybeRef expectType | Ref>(shallowRef(r)) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 9f7ebdad82..f068844100 100644 --- a/packages/reactivity/src/ref.ts +++ b/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. * @see {@link https://vuejs.org/api/reactivity-core.html#ref} */ -export function ref(value: T): T export function ref(value: T): Ref> export function ref(): Ref export function ref(value?: unknown) {