From c86e7ad11b9a235f24ce6e6804b0e52e76bd2897 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Wed, 19 Aug 2020 04:34:29 +0100 Subject: [PATCH] types(reactivity): improve typings for `shallowRef` (#1780) --- packages/reactivity/src/ref.ts | 5 ++++- test-dts/ref.test-d.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 396e34e4..83bef572 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -35,7 +35,10 @@ export function ref(value?: unknown) { return createRef(value) } -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(value?: unknown) { return createRef(value, true) diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index 6430b15c..c961f4be 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -1,6 +1,7 @@ import { Ref, ref, + shallowRef, isRef, unref, reactive, @@ -120,6 +121,22 @@ const state = reactive({ expectType(state.foo.label) +// shallowRef +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' +} + // proxyRefs: should return `reactive` directly const r1 = reactive({ k: 'v'