fix(types): fix shallowRef's return type (#12979)

close #12978
This commit is contained in:
Simon Lévesque 2023-12-06 10:51:56 -05:00 committed by GitHub
parent f5ef882a78
commit a174c29dab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 8 deletions

View File

@ -40,9 +40,7 @@ export function isRef(r: any): r is Ref {
return !!(r && (r as Ref).__v_isRef === true) return !!(r && (r as Ref).__v_isRef === true)
} }
export function ref<T extends object>( export function ref<T extends Ref>(value: T): T
value: T
): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
export function ref<T>(value: T): Ref<UnwrapRef<T>> export function ref<T>(value: T): Ref<UnwrapRef<T>>
export function ref<T = any>(): Ref<T | undefined> export function ref<T = any>(): Ref<T | undefined>
export function ref(value?: unknown) { export function ref(value?: unknown) {
@ -53,9 +51,8 @@ declare const ShallowRefMarker: unique symbol
export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true } export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }
export function shallowRef<T extends object>( export function shallowRef<T>(value: T | Ref<T>): Ref<T> | ShallowRef<T>
value: T export function shallowRef<T extends Ref>(value: T): T
): T extends Ref ? T : ShallowRef<T>
export function shallowRef<T>(value: T): ShallowRef<T> export function shallowRef<T>(value: T): ShallowRef<T>
export function shallowRef<T = any>(): ShallowRef<T | undefined> export function shallowRef<T = any>(): ShallowRef<T | undefined>
export function shallowRef(value?: unknown) { export function shallowRef(value?: unknown) {

View File

@ -15,7 +15,7 @@ import {
set, set,
del del
} from '../../index' } from '../../index'
import { describe, expectType } from '../utils' import { IsUnion, describe, expectType } from '../utils'
function plainType(arg: number | Ref<number>) { function plainType(arg: number | Ref<number>) {
// ref coercing // ref coercing
@ -385,3 +385,14 @@ describe('set/del', () => {
// @ts-expect-error // @ts-expect-error
del([], 'fse', 123) del([], 'fse', 123)
}) })
{
//#12978
type Steps = { step: '1' } | { step: '2' }
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
expectType<IsUnion<typeof shallowUnionGenParam>>(false)
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
}

View File

@ -1,4 +1,4 @@
import { ref, computed, watch } from '../../index' import { ref, computed, watch, shallowRef } from '../../index'
import { expectType } from '../utils' import { expectType } from '../utils'
const source = ref('foo') const source = ref('foo')
@ -76,3 +76,17 @@ watch([someRef, otherRef], values => {
// no type error // no type error
console.log(value2.a) console.log(value2.a)
}) })
{
//#12978
type Steps = { step: '1' } | { step: '2' }
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
watch(shallowUnionGenParam, value => {
expectType<Steps>(value)
})
watch(shallowUnionAsCast, value => {
expectType<Steps>(value)
})
}