From 7c448000b0def910c2cfabfdf7ff20a3d6bc844f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=A9vesque?= Date: Fri, 10 Nov 2023 00:40:47 -0500 Subject: [PATCH] fix(types): fix shallowRef return type with union value type (#7853) close #7852 --- packages/dts-test/ref.test-d.ts | 26 ++++++++++++++++++++++++-- packages/dts-test/watch.test-d.ts | 16 +++++++++++++++- packages/reactivity/src/ref.ts | 5 ++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index bbcde45ddda..542d9d6a9ef 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -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) { // ref coercing @@ -174,6 +175,27 @@ if (refStatus.value === 'initial') { refStatus.value = 'invalidating' } +{ + const shallow = shallowRef(1) + expectType>(shallow) + expectType>(shallow) +} + +{ + //#7852 + type Steps = { step: '1' } | { step: '2' } + const shallowUnionGenParam = shallowRef({ step: '1' }) + const shallowUnionAsCast = shallowRef({ step: '1' } as Steps) + + expectType>(false) + expectType>(false) +} + +describe('shallowRef with generic', () => { + const r = ref({}) as MaybeRef + expectType | Ref>(shallowRef(r)) +}) + // proxyRefs: should return `reactive` directly const r1 = reactive({ k: 'v' diff --git a/packages/dts-test/watch.test-d.ts b/packages/dts-test/watch.test-d.ts index 9d727999bcc..cc631d37acb 100644 --- a/packages/dts-test/watch.test-d.ts +++ b/packages/dts-test/watch.test-d.ts @@ -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({ step: '1' }) + const shallowUnionAsCast = shallowRef({ step: '1' } as Steps) + + watch(shallowUnionGenParam, value => { + expectType(value) + }) + watch(shallowUnionAsCast, value => { + expectType(value) + }) +} diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 915f5760878..68844310e64 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -115,9 +115,8 @@ export type ShallowRef = Ref & { [ShallowRefMarker]?: true } * @param value - The "inner value" for the shallow ref. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref} */ -export function shallowRef( - value: T -): T extends Ref ? T : ShallowRef +export function shallowRef(value: MaybeRef): Ref | ShallowRef +export function shallowRef(value: T): T export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) {