Skip to content

Commit 24eaa56

Browse files
authored
fix: update types to algin with vue-next (#653)
1 parent b3bb2e7 commit 24eaa56

File tree

3 files changed

+33
-51
lines changed

3 files changed

+33
-51
lines changed

src/apis/computed.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
1510
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
1611
readonly value: T
1712
}
1813

1914
export 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
2631
export 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
}

src/apis/index.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
export {
2-
del,
3-
isReactive,
4-
isRef,
5-
isRaw,
6-
markRaw,
7-
reactive,
8-
ref,
9-
customRef,
10-
Ref,
11-
set,
12-
shallowReactive,
13-
shallowRef,
14-
toRaw,
15-
toRef,
16-
toRefs,
17-
triggerRef,
18-
unref,
19-
UnwrapRef,
20-
isReadonly,
21-
shallowReadonly,
22-
proxyRefs,
23-
ShallowUnwrapRef,
24-
readonly,
25-
DeepReadonly,
26-
} from '../reactivity'
1+
export * from '../reactivity'
272
export * from './lifecycle'
283
export * from './watch'
294
export * from './computed'

src/reactivity/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export {
1010
ref,
1111
customRef,
1212
isRef,
13-
Ref,
1413
createRef,
15-
UnwrapRef,
1614
toRefs,
1715
toRef,
1816
unref,
1917
shallowRef,
2018
triggerRef,
2119
proxyRefs,
22-
ShallowUnwrapRef,
2320
} from './ref'
24-
export { readonly, isReadonly, shallowReadonly, DeepReadonly } from './readonly'
21+
export { readonly, isReadonly, shallowReadonly } from './readonly'
2522
export { set } from './set'
2623
export { del } from './del'
24+
25+
export type { Ref, ToRefs, UnwrapRef, ShallowUnwrapRef } from './ref'
26+
export type { DeepReadonly } from './readonly'

0 commit comments

Comments
 (0)