types: improve computed types (#343)

This commit is contained in:
Dmitry Sharshakov
2019-10-21 20:57:20 +03:00
committed by Evan You
parent 1f4937c2fd
commit 74d8c5919d
4 changed files with 24 additions and 18 deletions

View File

@@ -10,21 +10,24 @@ export interface WritableComputedRef<T> extends Ref<T> {
readonly effect: ReactiveEffect<T>
}
export type ComputedGetter<T> = () => T
export type ComputedSetter<T> = (v: T) => void
export interface WritableComputedOptions<T> {
get: () => T
set: (v: T) => void
get: ComputedGetter<T>
set: ComputedSetter<T>
}
export function computed<T>(getter: () => T): ComputedRef<T>
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>
): WritableComputedRef<T>
export function computed<T>(
getterOrOptions: (() => T) | WritableComputedOptions<T>
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
): any {
const isReadonly = isFunction(getterOrOptions)
const getter = isReadonly
? (getterOrOptions as (() => T))
? (getterOrOptions as ComputedGetter<T>)
: (getterOrOptions as WritableComputedOptions<T>).get
const setter = isReadonly
? __DEV__

View File

@@ -12,7 +12,9 @@ export {
computed,
ComputedRef,
WritableComputedRef,
WritableComputedOptions
WritableComputedOptions,
ComputedGetter,
ComputedSetter
} from './computed'
export {
effect,