Skip to content

Commit a174c29

Browse files
authored
fix(types): fix shallowRef's return type (#12979)
close #12978
1 parent f5ef882 commit a174c29

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/v3/reactivity/ref.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export function isRef(r: any): r is Ref {
4040
return !!(r && (r as Ref).__v_isRef === true)
4141
}
4242

43-
export function ref<T extends object>(
44-
value: T
45-
): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
43+
export function ref<T extends Ref>(value: T): T
4644
export function ref<T>(value: T): Ref<UnwrapRef<T>>
4745
export function ref<T = any>(): Ref<T | undefined>
4846
export function ref(value?: unknown) {
@@ -53,9 +51,8 @@ declare const ShallowRefMarker: unique symbol
5351

5452
export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }
5553

56-
export function shallowRef<T extends object>(
57-
value: T
58-
): T extends Ref ? T : ShallowRef<T>
54+
export function shallowRef<T>(value: T | Ref<T>): Ref<T> | ShallowRef<T>
55+
export function shallowRef<T extends Ref>(value: T): T
5956
export function shallowRef<T>(value: T): ShallowRef<T>
6057
export function shallowRef<T = any>(): ShallowRef<T | undefined>
6158
export function shallowRef(value?: unknown) {

types/test/v3/reactivity-test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
set,
1616
del
1717
} from '../../index'
18-
import { describe, expectType } from '../utils'
18+
import { IsUnion, describe, expectType } from '../utils'
1919

2020
function plainType(arg: number | Ref<number>) {
2121
// ref coercing
@@ -385,3 +385,14 @@ describe('set/del', () => {
385385
// @ts-expect-error
386386
del([], 'fse', 123)
387387
})
388+
389+
390+
{
391+
//#12978
392+
type Steps = { step: '1' } | { step: '2' }
393+
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
394+
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
395+
396+
expectType<IsUnion<typeof shallowUnionGenParam>>(false)
397+
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
398+
}

types/test/v3/watch-test.ts

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

44
const source = ref('foo')
@@ -76,3 +76,17 @@ watch([someRef, otherRef], values => {
7676
// no type error
7777
console.log(value2.a)
7878
})
79+
80+
{
81+
//#12978
82+
type Steps = { step: '1' } | { step: '2' }
83+
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
84+
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
85+
86+
watch(shallowUnionGenParam, value => {
87+
expectType<Steps>(value)
88+
})
89+
watch(shallowUnionAsCast, value => {
90+
expectType<Steps>(value)
91+
})
92+
}

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