@@ -7,32 +7,39 @@ import {
77 getVueInternalClasses ,
88} from '../utils'
99
10- interface Option < T > {
11- get : ( ) => T
12- set : ( value : T ) => void
13- }
14-
1510export interface ComputedRef < T = any > extends WritableComputedRef < T > {
1611 readonly value : T
1712}
1813
1914export interface WritableComputedRef < T > extends Ref < T > { }
2015
16+ export type ComputedGetter < T > = ( ctx ?: any ) => T
17+ export type ComputedSetter < T > = ( v : T ) => void
18+
19+ export interface WritableComputedOptions < T > {
20+ get : ComputedGetter < T >
21+ set : ComputedSetter < T >
22+ }
23+
2124// read-only
22- export function computed < T > ( getter : Option < T > [ 'get' ] ) : ComputedRef < T >
25+ export function computed < T > ( getter : ComputedGetter < T > ) : ComputedRef < T >
2326// writable
24- export function computed < T > ( options : Option < T > ) : WritableComputedRef < T >
27+ export function computed < T > (
28+ options : WritableComputedOptions < T >
29+ ) : WritableComputedRef < T >
2530// implement
2631export function computed < T > (
27- options : Option < T > [ 'get' ] | Option < T >
32+ getterOrOptions : ComputedGetter < T > | WritableComputedOptions < T >
2833) : ComputedRef < T > | WritableComputedRef < T > {
2934 const vm = getCurrentInstance ( ) ?. proxy
30- let get : Option < T > [ 'get' ] , set : Option < T > [ 'set' ] | undefined
31- if ( typeof options === 'function' ) {
32- get = options
35+ let getter : ComputedGetter < T >
36+ let setter : ComputedSetter < T > | undefined
37+
38+ if ( typeof getterOrOptions === 'function' ) {
39+ getter = getterOrOptions
3340 } else {
34- get = options . get
35- set = options . set
41+ getter = getterOrOptions . get
42+ setter = getterOrOptions . set
3643 }
3744
3845 let computedSetter
@@ -43,7 +50,7 @@ export function computed<T>(
4350 let watcher : any
4451 computedGetter = ( ) => {
4552 if ( ! watcher ) {
46- watcher = new Watcher ( vm , get , noopFn , { lazy : true } )
53+ watcher = new Watcher ( vm , getter , noopFn , { lazy : true } )
4754 }
4855 if ( watcher . dirty ) {
4956 watcher . evaluate ( )
@@ -55,22 +62,22 @@ export function computed<T>(
5562 }
5663
5764 computedSetter = ( v : T ) => {
58- if ( __DEV__ && ! set ) {
65+ if ( __DEV__ && ! setter ) {
5966 warn ( 'Write operation failed: computed value is readonly.' , vm ! )
6067 return
6168 }
6269
63- if ( set ) {
64- set ( v )
70+ if ( setter ) {
71+ setter ( v )
6572 }
6673 }
6774 } else {
6875 // fallback
6976 const computedHost = defineComponentInstance ( getVueConstructor ( ) , {
7077 computed : {
7178 $$state : {
72- get,
73- set,
79+ get : getter ,
80+ set : setter ,
7481 } ,
7582 } ,
7683 } )
@@ -79,7 +86,7 @@ export function computed<T>(
7986
8087 computedGetter = ( ) => ( computedHost as any ) . $$state
8188 computedSetter = ( v : T ) => {
82- if ( __DEV__ && ! set ) {
89+ if ( __DEV__ && ! setter ) {
8390 warn ( 'Write operation failed: computed value is readonly.' , vm ! )
8491 return
8592 }
@@ -93,6 +100,6 @@ export function computed<T>(
93100 get : computedGetter ,
94101 set : computedSetter ,
95102 } ,
96- ! set
103+ ! setter
97104 )
98105}
0 commit comments