From: 丶远方 Date: Sat, 16 Dec 2023 05:45:01 +0000 (+0800) Subject: fix(types): fix `shallowRef` type error (#9839) X-Git-Tag: v3.3.12~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9a571582b53220270e498d8712ea59312c0bef3a;p=thirdparty%2Fvuejs%2Fcore.git fix(types): fix `shallowRef` type error (#9839) --- diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index a467c446d0..f66ff92690 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -201,11 +201,23 @@ if (refStatus.value === 'initial') { expectType>(false) } -describe('shallowRef with generic', () => { - const r = ref({}) as MaybeRef - expectType | Ref>(shallowRef(r)) +describe('shallowRef with generic', () => { + const r = {} as T + const s = shallowRef(r) + expectType(s.value.name) + expectType>(shallowRef(r)) }) +{ + // should return ShallowRef | Ref, not ShallowRef> + expectType | Ref<{ name: string }>>( + shallowRef({} as MaybeRef<{ name: string }>) + ) + expectType | Ref | ShallowRef>( + shallowRef('' as Ref | string | number) + ) +} + // proxyRefs: should return `reactive` directly const r1 = reactive({ k: 'v' diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index f068844100..a85c79c0bc 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -114,9 +114,13 @@ 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: MaybeRef): 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)