From 90b912f0293c0f366e2efc921d8d5c1aeb92ea83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Sat, 10 Jun 2023 21:27:10 +0800 Subject: [PATCH 1/2] refactor(reactivity): refactor computed setter type --- packages/reactivity/src/computed.ts | 28 ++++++++++++++++++---------- packages/reactivity/src/effect.ts | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index b24484c9e62..3b7d555b940 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -11,19 +11,23 @@ export interface ComputedRef extends WritableComputedRef { [ComputedRefSymbol]: true } -export interface WritableComputedRef extends Ref { +export interface WritableComputedRef extends Ref { + get value(): T + set value(newValue: S | T) readonly effect: ReactiveEffect } +//The return type of a 'get' accessor must be assignable to its 'set' accessor type + export type ComputedGetter = (...args: any[]) => T export type ComputedSetter = (v: T) => void -export interface WritableComputedOptions { +export interface WritableComputedOptions { get: ComputedGetter - set: ComputedSetter + set: ComputedSetter } -export class ComputedRefImpl { +export class ComputedRefImpl { public dep?: Dep = undefined private _value!: T @@ -37,7 +41,7 @@ export class ComputedRefImpl { constructor( getter: ComputedGetter, - private readonly _setter: ComputedSetter, + private readonly _setter: ComputedSetter, isReadonly: boolean, isSSR: boolean ) { @@ -60,10 +64,10 @@ export class ComputedRefImpl { self._dirty = false self._value = self.effect.run()! } - return self._value + return self._value as any } - set value(newValue: T) { + set value(newValue: S) { this._setter(newValue) } } @@ -109,13 +113,17 @@ export function computed( options: WritableComputedOptions, debugOptions?: DebuggerOptions ): WritableComputedRef -export function computed( - getterOrOptions: ComputedGetter | WritableComputedOptions, +export function computed( + options: WritableComputedOptions, + debugOptions?: DebuggerOptions +): WritableComputedRef +export function computed( + getterOrOptions: ComputedGetter | WritableComputedOptions, debugOptions?: DebuggerOptions, isSSR = false ) { let getter: ComputedGetter - let setter: ComputedSetter + let setter: ComputedSetter const onlyGetter = isFunction(getterOrOptions) if (onlyGetter) { diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index d4a34edfef4..4ef045998dd 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -59,7 +59,7 @@ export class ReactiveEffect { * Can be attached after creation * @internal */ - computed?: ComputedRefImpl + computed?: ComputedRefImpl /** * @internal */ From f9f6b1ae1af33248e7f389b1633caeeeefb65dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Sun, 11 Jun 2023 16:09:15 +0800 Subject: [PATCH 2/2] chore: update setter type --- packages/reactivity/src/computed.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index 3b7d555b940..76064fa09f4 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -12,13 +12,12 @@ export interface ComputedRef extends WritableComputedRef { } export interface WritableComputedRef extends Ref { + // @ts-ignore get value(): T - set value(newValue: S | T) + set value(newValue: S) readonly effect: ReactiveEffect } -//The return type of a 'get' accessor must be assignable to its 'set' accessor type - export type ComputedGetter = (...args: any[]) => T export type ComputedSetter = (v: T) => void