wip: improve computed typing + test for setters

This commit is contained in:
Evan You
2019-08-21 12:01:05 -04:00
parent 8d99ab1ff8
commit 0aff144f93
6 changed files with 60 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
import { UnwrapNestedRefs, knownRefs } from './ref'
import { UnwrapNestedRefs, knownRefs, Ref } from './ref'
import { isFunction } from '@vue/shared'
export interface ComputedRef<T> {
@@ -12,9 +12,11 @@ export interface ComputedOptions<T> {
set: (v: T) => void
}
export function computed<T>(getter: () => T): ComputedRef<T>
export function computed<T>(options: ComputedOptions<T>): Ref<T>
export function computed<T>(
getterOrOptions: (() => T) | ComputedOptions<T>
): ComputedRef<T> {
): Ref<T> {
const isReadonly = isFunction(getterOrOptions)
const getter = isReadonly
? (getterOrOptions as (() => T))

View File

@@ -6,7 +6,7 @@ import {
immutableCollectionHandlers
} from './collectionHandlers'
import { UnwrapRef } from './ref'
import { UnwrapNestedRefs } from './ref'
import { ReactiveEffect } from './effect'
// The main WeakMap that stores {target -> key -> dep} connections.
@@ -40,9 +40,8 @@ const canObserve = (value: any): boolean => {
)
}
type ObservableFactory = <T>(target?: T) => UnwrapRef<T>
export const reactive = ((target: unknown): any => {
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
export function reactive(target: object) {
// if trying to observe an immutable proxy, return the immutable version.
if (immutableToRaw.has(target)) {
return target
@@ -58,9 +57,12 @@ export const reactive = ((target: unknown): any => {
mutableHandlers,
mutableCollectionHandlers
)
}) as ObservableFactory
}
export const immutable = ((target: unknown): any => {
export function immutable<T extends object>(
target: T
): Readonly<UnwrapNestedRefs<T>>
export function immutable(target: object) {
// value is a mutable observable, retrive its original and return
// a readonly version.
if (observedToRaw.has(target)) {
@@ -73,7 +75,7 @@ export const immutable = ((target: unknown): any => {
immutableHandlers,
immutableCollectionHandlers
)
}) as ObservableFactory
}
function createReactiveObject(
target: any,