Skip to content

Commit db729ce

Browse files
committed
feat(reactivity-transform/types): restructure macro types + export types for all shorthand methods
1 parent 198ca14 commit db729ce

File tree

7 files changed

+151
-109
lines changed

7 files changed

+151
-109
lines changed

packages/reactivity/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export {
1414
UnwrapRef,
1515
ShallowRef,
1616
ShallowUnwrapRef,
17-
RefUnwrapBailTypes
17+
RefUnwrapBailTypes,
18+
CustomRefFactory
1819
} from './ref'
1920
export {
2021
reactive,

packages/reactivity/src/ref.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export function proxyRefs<T extends object>(
151151
: new Proxy(objectWithRefs, shallowUnwrapHandlers)
152152
}
153153

154-
type CustomRefFactory<T> = (
154+
export type CustomRefFactory<T> = (
155155
track: () => void,
156156
trigger: () => void
157157
) => {

packages/runtime-core/src/index.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,28 @@ export {
151151
Ref,
152152
ToRef,
153153
ToRefs,
154-
ReactiveEffectOptions,
155-
DebuggerEvent,
156-
DebuggerOptions,
157-
TrackOpTypes,
158-
TriggerOpTypes,
159-
ComputedRef,
160-
WritableComputedRef,
161154
UnwrapRef,
155+
ShallowRef,
162156
ShallowUnwrapRef,
163-
WritableComputedOptions,
157+
RefUnwrapBailTypes,
158+
CustomRefFactory,
159+
ReactiveFlags,
164160
DeepReadonly,
165-
ShallowReactive
161+
ShallowReactive,
162+
UnwrapNestedRefs,
163+
ComputedRef,
164+
WritableComputedRef,
165+
WritableComputedOptions,
166+
ComputedGetter,
167+
ComputedSetter,
168+
ReactiveEffectRunner,
169+
ReactiveEffectOptions,
170+
EffectScheduler,
171+
DebuggerOptions,
172+
DebuggerEvent,
173+
DebuggerEventExtraInfo,
174+
TrackOpTypes,
175+
TriggerOpTypes
166176
} from '@vue/reactivity'
167177
export {
168178
WatchEffect,

packages/vue/macros-global.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
$ as _$,
3+
$$ as _$$,
4+
$ref as _$ref,
5+
$shallowRef as _$shallowRef,
6+
$computed as _$computed,
7+
$customRef as _$customRef,
8+
$toRef as _$toRef
9+
} from './macros'
10+
11+
declare global {
12+
const $: typeof _$
13+
const $$: typeof _$$
14+
const $ref: typeof _$ref
15+
const $shallowRef: typeof _$shallowRef
16+
const $computed: typeof _$computed
17+
const $customRef: typeof _$customRef
18+
const $toRef: typeof _$toRef
19+
}

packages/vue/macros.d.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {
2+
Ref,
3+
UnwrapRef,
4+
ComputedRef,
5+
WritableComputedOptions,
6+
DebuggerOptions,
7+
WritableComputedRef,
8+
CustomRefFactory
9+
} from '@vue/runtime-dom'
10+
11+
export declare const RefType: unique symbol
12+
13+
export declare const enum RefTypes {
14+
Ref = 1,
15+
ComputedRef = 2,
16+
WritableComputedRef = 3
17+
}
18+
19+
type RefValue<T> = T extends null | undefined
20+
? T
21+
: T & { [RefType]?: RefTypes.Ref }
22+
23+
type ComputedRefValue<T> = T extends null | undefined
24+
? T
25+
: T & { [RefType]?: RefTypes.ComputedRef }
26+
27+
type WritableComputedRefValue<T> = T extends null | undefined
28+
? T
29+
: T & { [RefType]?: RefTypes.WritableComputedRef }
30+
31+
type NormalObject<T extends object> = T & { [RefType]?: never }
32+
33+
/**
34+
* Vue ref transform macro for binding refs as reactive variables.
35+
*/
36+
export declare function $<T>(arg: ComputedRef<T>): ComputedRefValue<T>
37+
export declare function $<T>(
38+
arg: WritableComputedRef<T>
39+
): WritableComputedRefValue<T>
40+
export declare function $<T>(arg: Ref<T>): RefValue<T>
41+
export declare function $<T extends object>(arg?: T): DestructureRefs<T>
42+
43+
type DestructureRefs<T extends object> = {
44+
[K in keyof T]: T[K] extends ComputedRef<infer V>
45+
? ComputedRefValue<V>
46+
: T[K] extends WritableComputedRef<infer V>
47+
? WritableComputedRefValue<V>
48+
: T[K] extends Ref<infer V>
49+
? RefValue<V>
50+
: T[K]
51+
}
52+
53+
/**
54+
* Vue ref transform macro for accessing underlying refs of reactive varaibles.
55+
*/
56+
export declare function $$<T extends object>(arg: NormalObject<T>): ToRawRefs<T>
57+
export declare function $$<T>(value: RefValue<T>): Ref<T>
58+
export declare function $$<T>(value: ComputedRefValue<T>): ComputedRef<T>
59+
export declare function $$<T>(
60+
value: WritableComputedRefValue<T>
61+
): WritableComputedRef<T>
62+
63+
type ToRawRefs<T extends object> = {
64+
[K in keyof T]: T[K] extends RefValue<infer V>
65+
? Ref<V>
66+
: T[K] extends ComputedRefValue<infer V>
67+
? ComputedRef<V>
68+
: T[K] extends WritableComputedRefValue<infer V>
69+
? WritableComputedRef<V>
70+
: T[K] extends object
71+
? T[K] extends
72+
| Function
73+
| Map<any, any>
74+
| Set<any>
75+
| WeakMap<any, any>
76+
| WeakSet<any>
77+
? T[K]
78+
: ToRawRefs<T[K]>
79+
: T[K]
80+
}
81+
82+
export declare function $ref<T>(arg?: T | Ref<T>): RefValue<UnwrapRef<T>>
83+
84+
export declare function $shallowRef<T>(arg?: T): RefValue<T>
85+
86+
export declare function $toRef<T extends object, K extends keyof T>(
87+
object: T,
88+
key: K
89+
): RefValue<T[K]>
90+
91+
export declare function $toRef<T extends object, K extends keyof T>(
92+
object: T,
93+
key: K,
94+
defaultValue: T[K]
95+
): RefValue<Exclude<T[K], undefined>>
96+
97+
export declare function $customRef<T>(factory: CustomRefFactory<T>): RefValue<T>
98+
99+
export declare function $computed<T>(
100+
getter: () => T,
101+
debuggerOptions?: DebuggerOptions
102+
): ComputedRefValue<T>
103+
export declare function $computed<T>(
104+
options: WritableComputedOptions<T>,
105+
debuggerOptions?: DebuggerOptions
106+
): WritableComputedRefValue<T>

packages/vue/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
},
3535
"./dist/*": "./dist/*",
3636
"./package.json": "./package.json",
37+
"./macros": "./macros.d.ts",
38+
"./macros-global": "./macros-global.d.ts",
3739
"./ref-macros": "./ref-macros.d.ts"
3840
},
3941
"buildOptions": {

packages/vue/ref-macros.d.ts

+2-98
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,2 @@
1-
import {
2-
Ref,
3-
UnwrapRef,
4-
ComputedRef,
5-
WritableComputedOptions,
6-
DebuggerOptions,
7-
WritableComputedRef
8-
} from '@vue/runtime-dom'
9-
10-
declare const RefType: unique symbol
11-
12-
declare const enum RefTypes {
13-
Ref = 1,
14-
ComputedRef = 2,
15-
WritableComputedRef = 3
16-
}
17-
18-
type RefValue<T> = T extends null | undefined
19-
? T
20-
: T & { [RefType]?: RefTypes.Ref }
21-
22-
type ComputedRefValue<T> = T extends null | undefined
23-
? T
24-
: T & { [RefType]?: RefTypes.ComputedRef }
25-
26-
type WritableComputedRefValue<T> = T extends null | undefined
27-
? T
28-
: T & { [RefType]?: RefTypes.WritableComputedRef }
29-
30-
type NormalObject<T extends object> = T & { [RefType]?: never }
31-
32-
/**
33-
* Vue ref transform macro for binding refs as reactive variables.
34-
*/
35-
declare function _$<T>(arg: ComputedRef<T>): ComputedRefValue<T>
36-
declare function _$<T>(arg: WritableComputedRef<T>): WritableComputedRefValue<T>
37-
declare function _$<T>(arg: Ref<T>): RefValue<T>
38-
declare function _$<T extends object>(arg?: T): DestructureRefs<T>
39-
40-
type DestructureRefs<T extends object> = {
41-
[K in keyof T]: T[K] extends ComputedRef<infer V>
42-
? ComputedRefValue<V>
43-
: T[K] extends WritableComputedRef<infer V>
44-
? WritableComputedRefValue<V>
45-
: T[K] extends Ref<infer V>
46-
? RefValue<V>
47-
: T[K]
48-
}
49-
50-
/**
51-
* Vue ref transform macro for accessing underlying refs of reactive varaibles.
52-
*/
53-
declare function _$$<T extends object>(arg: NormalObject<T>): ToRawRefs<T>
54-
declare function _$$<T>(value: RefValue<T>): Ref<T>
55-
declare function _$$<T>(value: ComputedRefValue<T>): ComputedRef<T>
56-
declare function _$$<T>(
57-
value: WritableComputedRefValue<T>
58-
): WritableComputedRef<T>
59-
60-
type ToRawRefs<T extends object> = {
61-
[K in keyof T]: T[K] extends RefValue<infer V>
62-
? Ref<V>
63-
: T[K] extends ComputedRefValue<infer V>
64-
? ComputedRef<V>
65-
: T[K] extends WritableComputedRefValue<infer V>
66-
? WritableComputedRef<V>
67-
: T[K] extends object
68-
? T[K] extends
69-
| Function
70-
| Map<any, any>
71-
| Set<any>
72-
| WeakMap<any, any>
73-
| WeakSet<any>
74-
? T[K]
75-
: ToRawRefs<T[K]>
76-
: T[K]
77-
}
78-
79-
declare function _$ref<T>(arg?: T | Ref<T>): RefValue<UnwrapRef<T>>
80-
81-
declare function _$shallowRef<T>(arg?: T): RefValue<T>
82-
83-
declare function _$computed<T>(
84-
getter: () => T,
85-
debuggerOptions?: DebuggerOptions
86-
): ComputedRefValue<T>
87-
declare function _$computed<T>(
88-
options: WritableComputedOptions<T>,
89-
debuggerOptions?: DebuggerOptions
90-
): WritableComputedRefValue<T>
91-
92-
declare global {
93-
const $: typeof _$
94-
const $$: typeof _$$
95-
const $ref: typeof _$ref
96-
const $shallowRef: typeof _$shallowRef
97-
const $computed: typeof _$computed
98-
}
1+
// TODO deprecated file - to be removed when out of experimental
2+
import './macros-global'

0 commit comments

Comments
 (0)