From a174c29dab2cf655b06f7870e0ac5a78ef35ec8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=A9vesque?= Date: Wed, 6 Dec 2023 10:51:56 -0500 Subject: [PATCH] fix(types): fix shallowRef's return type (#12979) close #12978 --- src/v3/reactivity/ref.ts | 9 +++------ types/test/v3/reactivity-test.ts | 13 ++++++++++++- types/test/v3/watch-test.ts | 16 +++++++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/v3/reactivity/ref.ts b/src/v3/reactivity/ref.ts index 244acc9b0..33495806d 100644 --- a/src/v3/reactivity/ref.ts +++ b/src/v3/reactivity/ref.ts @@ -40,9 +40,7 @@ export function isRef(r: any): r is Ref { return !!(r && (r as Ref).__v_isRef === true) } -export function ref( - value: T -): [T] extends [Ref] ? T : Ref> +export function ref(value: T): T export function ref(value: T): Ref> export function ref(): Ref export function ref(value?: unknown) { @@ -53,9 +51,8 @@ declare const ShallowRefMarker: unique symbol export type ShallowRef = Ref & { [ShallowRefMarker]?: true } -export function shallowRef( - value: T -): T extends Ref ? T : ShallowRef +export function shallowRef(value: T | Ref): Ref | ShallowRef +export function shallowRef(value: T): T export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { diff --git a/types/test/v3/reactivity-test.ts b/types/test/v3/reactivity-test.ts index dd1b2a4d8..c357bf8d5 100644 --- a/types/test/v3/reactivity-test.ts +++ b/types/test/v3/reactivity-test.ts @@ -15,7 +15,7 @@ import { set, del } from '../../index' -import { describe, expectType } from '../utils' +import { IsUnion, describe, expectType } from '../utils' function plainType(arg: number | Ref) { // ref coercing @@ -385,3 +385,14 @@ describe('set/del', () => { // @ts-expect-error del([], 'fse', 123) }) + + +{ + //#12978 + type Steps = { step: '1' } | { step: '2' } + const shallowUnionGenParam = shallowRef({ step: '1' }) + const shallowUnionAsCast = shallowRef({ step: '1' } as Steps) + + expectType>(false) + expectType>(false) +} \ No newline at end of file diff --git a/types/test/v3/watch-test.ts b/types/test/v3/watch-test.ts index 388fdc2ad..aeb5ff36c 100644 --- a/types/test/v3/watch-test.ts +++ b/types/test/v3/watch-test.ts @@ -1,4 +1,4 @@ -import { ref, computed, watch } from '../../index' +import { ref, computed, watch, shallowRef } from '../../index' import { expectType } from '../utils' const source = ref('foo') @@ -76,3 +76,17 @@ watch([someRef, otherRef], values => { // no type error console.log(value2.a) }) + +{ + //#12978 + 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) + }) +} \ No newline at end of file