From 474d8156a81813048bf888f1e720d1640596b7b9 Mon Sep 17 00:00:00 2001 From: pikax Date: Wed, 5 Aug 2020 09:18:04 +0100 Subject: [PATCH] types: improve typings for `shallowRef` --- src/reactivity/ref.ts | 5 ++++- test-dts/ref.test-d.ts | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/reactivity/ref.ts b/src/reactivity/ref.ts index a3e3242d..321fce4c 100644 --- a/src/reactivity/ref.ts +++ b/src/reactivity/ref.ts @@ -167,7 +167,10 @@ export function toRef( }) } -export function shallowRef(value: T): T extends Ref ? T : Ref +export function shallowRef( + value: T +): T extends Ref ? T : Ref +export function shallowRef(value: T): Ref export function shallowRef(): Ref export function shallowRef(raw?: unknown) { if (isRef(raw)) { diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index f15201df..79d5f434 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -1,4 +1,12 @@ -import { Ref, ref, isRef, unref, reactive, expectType } from './index' +import { + Ref, + ref, + shallowRef, + isRef, + unref, + reactive, + expectType, +} from './index' function plainType(arg: number | Ref) { // ref coercing @@ -92,3 +100,18 @@ const state = reactive({ }) expectType(state.foo.label) + +type Status = 'initial' | 'ready' | 'invalidating' +const shallowStatus = shallowRef('initial') +if (shallowStatus.value === 'initial') { + expectType>(shallowStatus) + expectType(shallowStatus.value) + shallowStatus.value = 'invalidating' +} + +const refStatus = ref('initial') +if (refStatus.value === 'initial') { + expectType>(shallowStatus) + expectType(shallowStatus.value) + refStatus.value = 'invalidating' +}