From 24eaa56797160114bc88aa2e138827a8b0a966b5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 3 Mar 2021 14:06:07 +0800 Subject: [PATCH] fix: update types to algin with vue-next (#653) --- src/apis/computed.ts | 49 +++++++++++++++++++++++------------------ src/apis/index.ts | 27 +---------------------- src/reactivity/index.ts | 8 +++---- 3 files changed, 33 insertions(+), 51 deletions(-) diff --git a/src/apis/computed.ts b/src/apis/computed.ts index d2042dea..9ac0b28b 100644 --- a/src/apis/computed.ts +++ b/src/apis/computed.ts @@ -7,32 +7,39 @@ import { getVueInternalClasses, } from '../utils' -interface Option { - get: () => T - set: (value: T) => void -} - export interface ComputedRef extends WritableComputedRef { readonly value: T } export interface WritableComputedRef extends Ref {} +export type ComputedGetter = (ctx?: any) => T +export type ComputedSetter = (v: T) => void + +export interface WritableComputedOptions { + get: ComputedGetter + set: ComputedSetter +} + // read-only -export function computed(getter: Option['get']): ComputedRef +export function computed(getter: ComputedGetter): ComputedRef // writable -export function computed(options: Option): WritableComputedRef +export function computed( + options: WritableComputedOptions +): WritableComputedRef // implement export function computed( - options: Option['get'] | Option + getterOrOptions: ComputedGetter | WritableComputedOptions ): ComputedRef | WritableComputedRef { const vm = getCurrentInstance()?.proxy - let get: Option['get'], set: Option['set'] | undefined - if (typeof options === 'function') { - get = options + let getter: ComputedGetter + let setter: ComputedSetter | undefined + + if (typeof getterOrOptions === 'function') { + getter = getterOrOptions } else { - get = options.get - set = options.set + getter = getterOrOptions.get + setter = getterOrOptions.set } let computedSetter @@ -43,7 +50,7 @@ export function computed( let watcher: any computedGetter = () => { if (!watcher) { - watcher = new Watcher(vm, get, noopFn, { lazy: true }) + watcher = new Watcher(vm, getter, noopFn, { lazy: true }) } if (watcher.dirty) { watcher.evaluate() @@ -55,13 +62,13 @@ export function computed( } computedSetter = (v: T) => { - if (__DEV__ && !set) { + if (__DEV__ && !setter) { warn('Write operation failed: computed value is readonly.', vm!) return } - if (set) { - set(v) + if (setter) { + setter(v) } } } else { @@ -69,8 +76,8 @@ export function computed( const computedHost = defineComponentInstance(getVueConstructor(), { computed: { $$state: { - get, - set, + get: getter, + set: setter, }, }, }) @@ -79,7 +86,7 @@ export function computed( computedGetter = () => (computedHost as any).$$state computedSetter = (v: T) => { - if (__DEV__ && !set) { + if (__DEV__ && !setter) { warn('Write operation failed: computed value is readonly.', vm!) return } @@ -93,6 +100,6 @@ export function computed( get: computedGetter, set: computedSetter, }, - !set + !setter ) } diff --git a/src/apis/index.ts b/src/apis/index.ts index 5eb81ac4..19b715de 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -1,29 +1,4 @@ -export { - del, - isReactive, - isRef, - isRaw, - markRaw, - reactive, - ref, - customRef, - Ref, - set, - shallowReactive, - shallowRef, - toRaw, - toRef, - toRefs, - triggerRef, - unref, - UnwrapRef, - isReadonly, - shallowReadonly, - proxyRefs, - ShallowUnwrapRef, - readonly, - DeepReadonly, -} from '../reactivity' +export * from '../reactivity' export * from './lifecycle' export * from './watch' export * from './computed' diff --git a/src/reactivity/index.ts b/src/reactivity/index.ts index b045e915..c7a9424e 100644 --- a/src/reactivity/index.ts +++ b/src/reactivity/index.ts @@ -10,17 +10,17 @@ export { ref, customRef, isRef, - Ref, createRef, - UnwrapRef, toRefs, toRef, unref, shallowRef, triggerRef, proxyRefs, - ShallowUnwrapRef, } from './ref' -export { readonly, isReadonly, shallowReadonly, DeepReadonly } from './readonly' +export { readonly, isReadonly, shallowReadonly } from './readonly' export { set } from './set' export { del } from './del' + +export type { Ref, ToRefs, UnwrapRef, ShallowUnwrapRef } from './ref' +export type { DeepReadonly } from './readonly'