From: Carlos Rodrigues Date: Wed, 19 Aug 2020 03:34:29 +0000 (+0100) Subject: types(reactivity): improve typings for `shallowRef` (#1780) X-Git-Tag: v3.0.0-rc.6~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c86e7ad11b9a235f24ce6e6804b0e52e76bd2897;p=thirdparty%2Fvuejs%2Fcore.git types(reactivity): improve typings for `shallowRef` (#1780) --- diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 396e34e47d..83bef57223 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 6430b15c57..c961f4be37 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'