Pārlūkot izejas kodu

types: improve computed types (#343)

Dmitry Sharshakov 6 gadi atpakaļ
vecāks
revīzija
74d8c5919d

+ 8 - 5
packages/reactivity/src/computed.ts

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

+ 3 - 1
packages/reactivity/src/index.ts

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

+ 9 - 9
packages/runtime-core/src/apiOptions.ts

@@ -30,7 +30,11 @@ import {
   DebuggerHook,
   DebuggerHook,
   ErrorCapturedHook
   ErrorCapturedHook
 } from './apiLifecycle'
 } from './apiLifecycle'
-import { reactive } from '@vue/reactivity'
+import {
+  reactive,
+  ComputedGetter,
+  WritableComputedOptions
+} from '@vue/reactivity'
 import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
 import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
 import { Directive } from './directives'
 import { Directive } from './directives'
 import { ComponentPublicInstance } from './componentProxy'
 import { ComponentPublicInstance } from './componentProxy'
@@ -100,14 +104,10 @@ export type ComponentOptions =
 // TODO legacy component definition also supports constructors with .options
 // TODO legacy component definition also supports constructors with .options
 type LegacyComponent = ComponentOptions
 type LegacyComponent = ComponentOptions
 
 
-export interface ComputedOptions {
-  [key: string]:
-    | Function
-    | {
-        get: Function
-        set: Function
-      }
-}
+export type ComputedOptions = Record<
+  string,
+  ComputedGetter<any> | WritableComputedOptions<any>
+>
 
 
 export interface MethodOptions {
 export interface MethodOptions {
   [key: string]: Function
   [key: string]: Function

+ 4 - 3
packages/runtime-core/src/apiReactivity.ts

@@ -26,7 +26,8 @@ import {
   ComputedRef,
   ComputedRef,
   WritableComputedOptions,
   WritableComputedOptions,
   ReactiveEffect,
   ReactiveEffect,
-  WritableComputedRef
+  WritableComputedRef,
+  ComputedGetter
 } from '@vue/reactivity'
 } from '@vue/reactivity'
 
 
 import { currentInstance } from './component'
 import { currentInstance } from './component'
@@ -39,12 +40,12 @@ export function recordEffect(effect: ReactiveEffect) {
   }
   }
 }
 }
 
 
-export function computed<T>(getter: () => T): ComputedRef<T>
+export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
 export function computed<T>(
 export function computed<T>(
   options: WritableComputedOptions<T>
   options: WritableComputedOptions<T>
 ): WritableComputedRef<T>
 ): WritableComputedRef<T>
 export function computed<T>(
 export function computed<T>(
-  getterOrOptions: (() => T) | WritableComputedOptions<T>
+  getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
 ) {
 ) {
   const c = _computed(getterOrOptions as any)
   const c = _computed(getterOrOptions as any)
   recordEffect(c.effect)
   recordEffect(c.effect)