Skip to content

Commit 7c44800

Browse files
authored
fix(types): fix shallowRef return type with union value type (#7853)
close #7852
1 parent 364f319 commit 7c44800

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

packages/dts-test/ref.test-d.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {
1515
MaybeRef,
1616
MaybeRefOrGetter,
1717
ComputedRef,
18-
computed
18+
computed,
19+
ShallowRef
1920
} from 'vue'
20-
import { expectType, describe } from './utils'
21+
import { expectType, describe, IsUnion } from './utils'
2122

2223
function plainType(arg: number | Ref<number>) {
2324
// ref coercing
@@ -174,6 +175,27 @@ if (refStatus.value === 'initial') {
174175
refStatus.value = 'invalidating'
175176
}
176177

178+
{
179+
const shallow = shallowRef(1)
180+
expectType<Ref<number>>(shallow)
181+
expectType<ShallowRef<number>>(shallow)
182+
}
183+
184+
{
185+
//#7852
186+
type Steps = { step: '1' } | { step: '2' }
187+
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
188+
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
189+
190+
expectType<IsUnion<typeof shallowUnionGenParam>>(false)
191+
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
192+
}
193+
194+
describe('shallowRef with generic', <T>() => {
195+
const r = ref({}) as MaybeRef<T>
196+
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
197+
})
198+
177199
// proxyRefs: should return `reactive` directly
178200
const r1 = reactive({
179201
k: 'v'

packages/dts-test/watch.test-d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, computed, watch, defineComponent } from 'vue'
1+
import { ref, computed, watch, defineComponent, shallowRef } from 'vue'
22
import { expectType } from './utils'
33

44
const source = ref('foo')
@@ -92,3 +92,17 @@ defineComponent({
9292
)
9393
}
9494
})
95+
96+
{
97+
//#7852
98+
type Steps = { step: '1' } | { step: '2' }
99+
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
100+
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
101+
102+
watch(shallowUnionGenParam, value => {
103+
expectType<Steps>(value)
104+
})
105+
watch(shallowUnionAsCast, value => {
106+
expectType<Steps>(value)
107+
})
108+
}

packages/reactivity/src/ref.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }
115115
* @param value - The "inner value" for the shallow ref.
116116
* @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
117117
*/
118-
export function shallowRef<T extends object>(
119-
value: T
120-
): T extends Ref ? T : ShallowRef<T>
118+
export function shallowRef<T>(value: MaybeRef<T>): Ref<T> | ShallowRef<T>
119+
export function shallowRef<T extends Ref>(value: T): T
121120
export function shallowRef<T>(value: T): ShallowRef<T>
122121
export function shallowRef<T = any>(): ShallowRef<T | undefined>
123122
export function shallowRef(value?: unknown) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy