From 7da94ea0dbe7a1bfe3cd2f63012ef94f64bcfa1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Fri, 15 Dec 2023 22:43:56 +0800 Subject: [PATCH 1/2] fix(types): fix `shallowRef` type error --- src/v3/reactivity/ref.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/v3/reactivity/ref.ts b/src/v3/reactivity/ref.ts index 33495806da..9a1d441adc 100644 --- a/src/v3/reactivity/ref.ts +++ b/src/v3/reactivity/ref.ts @@ -51,9 +51,13 @@ declare const ShallowRefMarker: unique symbol export type ShallowRef = Ref & { [ShallowRefMarker]?: true } -export function shallowRef(value: T | Ref): Ref | ShallowRef -export function shallowRef(value: T): T -export function shallowRef(value: T): ShallowRef +export function shallowRef( + value: T +): Ref extends T + ? T extends Ref + ? IfAny, T> + : ShallowRef + : ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { return createRef(value, true) From 29e09f3ce09fa889235a3edba64650cbbadb84f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Fri, 15 Dec 2023 23:05:02 +0800 Subject: [PATCH 2/2] test(types): add test case --- types/test/v3/reactivity-test.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/types/test/v3/reactivity-test.ts b/types/test/v3/reactivity-test.ts index c357bf8d5c..7ce3b9db62 100644 --- a/types/test/v3/reactivity-test.ts +++ b/types/test/v3/reactivity-test.ts @@ -13,9 +13,10 @@ import { markRaw, shallowReadonly, set, - del + del, + ShallowRef } from '../../index' -import { IsUnion, describe, expectType } from '../utils' +import { IsUnion, describe, expectType, expectError } from '../utils' function plainType(arg: number | Ref) { // ref coercing @@ -163,6 +164,15 @@ if (shallowStatus.value === 'initial') { shallowStatus.value = 'invalidating' } +{ + // should return ShallowRef | Ref, not ShallowRef> + expectType | Ref<{ name: string }>>( + shallowRef({} as { name: string } | Ref<{ name: string }>) + ) + expectType | Ref | ShallowRef>( + shallowRef('' as Ref | string | number) + ) +} const refStatus = ref('initial') if (refStatus.value === 'initial') { expectType>(shallowStatus) @@ -386,7 +396,6 @@ describe('set/del', () => { del([], 'fse', 123) }) - { //#12978 type Steps = { step: '1' } | { step: '2' } @@ -395,4 +404,4 @@ describe('set/del', () => { expectType>(false) expectType>(false) -} \ No newline at end of file +}