types(reactivity): improve typings for shallowRef
(#1780)
This commit is contained in:
parent
4172fdb90c
commit
c86e7ad11b
@ -35,7 +35,10 @@ export function ref(value?: unknown) {
|
|||||||
return createRef(value)
|
return createRef(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
|
export function shallowRef<T extends object>(
|
||||||
|
value: T
|
||||||
|
): T extends Ref ? T : Ref<T>
|
||||||
|
export function shallowRef<T>(value: T): Ref<T>
|
||||||
export function shallowRef<T = any>(): Ref<T | undefined>
|
export function shallowRef<T = any>(): Ref<T | undefined>
|
||||||
export function shallowRef(value?: unknown) {
|
export function shallowRef(value?: unknown) {
|
||||||
return createRef(value, true)
|
return createRef(value, true)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
Ref,
|
Ref,
|
||||||
ref,
|
ref,
|
||||||
|
shallowRef,
|
||||||
isRef,
|
isRef,
|
||||||
unref,
|
unref,
|
||||||
reactive,
|
reactive,
|
||||||
@ -120,6 +121,22 @@ const state = reactive({
|
|||||||
|
|
||||||
expectType<string>(state.foo.label)
|
expectType<string>(state.foo.label)
|
||||||
|
|
||||||
|
// shallowRef
|
||||||
|
type Status = 'initial' | 'ready' | 'invalidating'
|
||||||
|
const shallowStatus = shallowRef<Status>('initial')
|
||||||
|
if (shallowStatus.value === 'initial') {
|
||||||
|
expectType<Ref<Status>>(shallowStatus)
|
||||||
|
expectType<Status>(shallowStatus.value)
|
||||||
|
shallowStatus.value = 'invalidating'
|
||||||
|
}
|
||||||
|
|
||||||
|
const refStatus = ref<Status>('initial')
|
||||||
|
if (refStatus.value === 'initial') {
|
||||||
|
expectType<Ref<Status>>(shallowStatus)
|
||||||
|
expectType<Status>(shallowStatus.value)
|
||||||
|
refStatus.value = 'invalidating'
|
||||||
|
}
|
||||||
|
|
||||||
// proxyRefs: should return `reactive` directly
|
// proxyRefs: should return `reactive` directly
|
||||||
const r1 = reactive({
|
const r1 = reactive({
|
||||||
k: 'v'
|
k: 'v'
|
||||||
|
Loading…
Reference in New Issue
Block a user