From 3411b8de362bc258a8af79f7f81b20d11b47d9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=A9vesque?= Date: Tue, 7 Mar 2023 16:55:30 -0500 Subject: [PATCH 1/5] Fixes shallowRef's return type Will update with a link to the issue in the upstream repo. --- packages/reactivity/src/ref.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 9bb4ce29be5..7020d619474 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -89,7 +89,7 @@ export type ShallowRef = Ref & { [ShallowRefMarker]?: true } export function shallowRef( value: T -): T extends Ref ? T : ShallowRef +): [T] extends [Ref] ? T : ShallowRef export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { From 0a789c191ffe1e23dd474acbef5789d8b9d5c1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=A9vesque?= Date: Wed, 8 Mar 2023 13:45:54 -0500 Subject: [PATCH 2/5] chore: dts-test #7852 --- packages/dts-test/ref.test-d.ts | 21 +++++++++++++++++++-- packages/dts-test/watch.test-d.ts | 16 +++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index dbf54de09c8..d41d129049a 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -10,9 +10,10 @@ import { toRefs, ToRefs, shallowReactive, - readonly + readonly, + ShallowRef } from 'vue' -import { expectType, describe } from './utils' +import { expectType, describe, IsUnion } from './utils' function plainType(arg: number | Ref) { // ref coercing @@ -167,6 +168,22 @@ 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) +} + // 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) + }) +} From 5469bb1d4f39512101b55bdc5a0d1ba74ee4d425 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 24 Oct 2023 16:47:01 +0100 Subject: [PATCH 3/5] fix(types): support generics, fix vueuse integration --- packages/dts-test/ref.test-d.ts | 5 +++++ packages/reactivity/src/ref.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index aff770928cf..542d9d6a9ef 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -191,6 +191,11 @@ if (refStatus.value === 'initial') { 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/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index ed547f40485..d3d032852d9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -118,6 +118,7 @@ export type ShallowRef = Ref & { [ShallowRefMarker]?: true } export function shallowRef( value: T ): [T] extends [Ref] ? T : ShallowRef +export function shallowRef(value: MaybeRef): Ref | ShallowRef export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { From 96bb6283875e76bbb99a5f425f46e8882a0f5d8d Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 24 Oct 2023 16:59:34 +0100 Subject: [PATCH 4/5] chore: move MaybeRef overflow priority --- packages/reactivity/src/ref.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index d3d032852d9..e277e9384d1 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -115,10 +115,10 @@ 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] extends [Ref] ? T : ShallowRef -export function shallowRef(value: MaybeRef): Ref | ShallowRef export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { From 0d18686beecfc984bf70449041cbc3c99b611458 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 10 Nov 2023 13:34:33 +0800 Subject: [PATCH 5/5] Update ref.ts --- packages/reactivity/src/ref.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index e277e9384d1..68844310e64 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -116,9 +116,7 @@ export type ShallowRef = Ref & { [ShallowRefMarker]?: true } * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref} */ export function shallowRef(value: MaybeRef): Ref | ShallowRef -export function shallowRef( - value: T -): [T] extends [Ref] ? T : ShallowRef +export function shallowRef(value: T): T export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) {