From db729ce99eb13cd18dad600055239c63edd9cfb8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sat, 11 Dec 2021 18:15:44 +0800 Subject: [PATCH] feat(reactivity-transform/types): restructure macro types + export types for all shorthand methods --- packages/reactivity/src/index.ts | 3 +- packages/reactivity/src/ref.ts | 2 +- packages/runtime-core/src/index.ts | 28 +++++--- packages/vue/macros-global.d.ts | 19 ++++++ packages/vue/macros.d.ts | 106 +++++++++++++++++++++++++++++ packages/vue/package.json | 2 + packages/vue/ref-macros.d.ts | 100 +-------------------------- 7 files changed, 151 insertions(+), 109 deletions(-) create mode 100644 packages/vue/macros-global.d.ts create mode 100644 packages/vue/macros.d.ts diff --git a/packages/reactivity/src/index.ts b/packages/reactivity/src/index.ts index c61999bd6f3..676f8598fb3 100644 --- a/packages/reactivity/src/index.ts +++ b/packages/reactivity/src/index.ts @@ -14,7 +14,8 @@ export { UnwrapRef, ShallowRef, ShallowUnwrapRef, - RefUnwrapBailTypes + RefUnwrapBailTypes, + CustomRefFactory } from './ref' export { reactive, diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 66c3dd43982..55059485d6c 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -151,7 +151,7 @@ export function proxyRefs( : new Proxy(objectWithRefs, shallowUnwrapHandlers) } -type CustomRefFactory = ( +export type CustomRefFactory = ( track: () => void, trigger: () => void ) => { diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index ff0aeb90df4..63aac5ac90f 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -151,18 +151,28 @@ export { Ref, ToRef, ToRefs, - ReactiveEffectOptions, - DebuggerEvent, - DebuggerOptions, - TrackOpTypes, - TriggerOpTypes, - ComputedRef, - WritableComputedRef, UnwrapRef, + ShallowRef, ShallowUnwrapRef, - WritableComputedOptions, + RefUnwrapBailTypes, + CustomRefFactory, + ReactiveFlags, DeepReadonly, - ShallowReactive + ShallowReactive, + UnwrapNestedRefs, + ComputedRef, + WritableComputedRef, + WritableComputedOptions, + ComputedGetter, + ComputedSetter, + ReactiveEffectRunner, + ReactiveEffectOptions, + EffectScheduler, + DebuggerOptions, + DebuggerEvent, + DebuggerEventExtraInfo, + TrackOpTypes, + TriggerOpTypes } from '@vue/reactivity' export { WatchEffect, diff --git a/packages/vue/macros-global.d.ts b/packages/vue/macros-global.d.ts new file mode 100644 index 00000000000..9b6f5a5392f --- /dev/null +++ b/packages/vue/macros-global.d.ts @@ -0,0 +1,19 @@ +import { + $ as _$, + $$ as _$$, + $ref as _$ref, + $shallowRef as _$shallowRef, + $computed as _$computed, + $customRef as _$customRef, + $toRef as _$toRef +} from './macros' + +declare global { + const $: typeof _$ + const $$: typeof _$$ + const $ref: typeof _$ref + const $shallowRef: typeof _$shallowRef + const $computed: typeof _$computed + const $customRef: typeof _$customRef + const $toRef: typeof _$toRef +} diff --git a/packages/vue/macros.d.ts b/packages/vue/macros.d.ts new file mode 100644 index 00000000000..a0a9f54d306 --- /dev/null +++ b/packages/vue/macros.d.ts @@ -0,0 +1,106 @@ +import { + Ref, + UnwrapRef, + ComputedRef, + WritableComputedOptions, + DebuggerOptions, + WritableComputedRef, + CustomRefFactory +} from '@vue/runtime-dom' + +export declare const RefType: unique symbol + +export declare const enum RefTypes { + Ref = 1, + ComputedRef = 2, + WritableComputedRef = 3 +} + +type RefValue = T extends null | undefined + ? T + : T & { [RefType]?: RefTypes.Ref } + +type ComputedRefValue = T extends null | undefined + ? T + : T & { [RefType]?: RefTypes.ComputedRef } + +type WritableComputedRefValue = T extends null | undefined + ? T + : T & { [RefType]?: RefTypes.WritableComputedRef } + +type NormalObject = T & { [RefType]?: never } + +/** + * Vue ref transform macro for binding refs as reactive variables. + */ +export declare function $(arg: ComputedRef): ComputedRefValue +export declare function $( + arg: WritableComputedRef +): WritableComputedRefValue +export declare function $(arg: Ref): RefValue +export declare function $(arg?: T): DestructureRefs + +type DestructureRefs = { + [K in keyof T]: T[K] extends ComputedRef + ? ComputedRefValue + : T[K] extends WritableComputedRef + ? WritableComputedRefValue + : T[K] extends Ref + ? RefValue + : T[K] +} + +/** + * Vue ref transform macro for accessing underlying refs of reactive varaibles. + */ +export declare function $$(arg: NormalObject): ToRawRefs +export declare function $$(value: RefValue): Ref +export declare function $$(value: ComputedRefValue): ComputedRef +export declare function $$( + value: WritableComputedRefValue +): WritableComputedRef + +type ToRawRefs = { + [K in keyof T]: T[K] extends RefValue + ? Ref + : T[K] extends ComputedRefValue + ? ComputedRef + : T[K] extends WritableComputedRefValue + ? WritableComputedRef + : T[K] extends object + ? T[K] extends + | Function + | Map + | Set + | WeakMap + | WeakSet + ? T[K] + : ToRawRefs + : T[K] +} + +export declare function $ref(arg?: T | Ref): RefValue> + +export declare function $shallowRef(arg?: T): RefValue + +export declare function $toRef( + object: T, + key: K +): RefValue + +export declare function $toRef( + object: T, + key: K, + defaultValue: T[K] +): RefValue> + +export declare function $customRef(factory: CustomRefFactory): RefValue + +export declare function $computed( + getter: () => T, + debuggerOptions?: DebuggerOptions +): ComputedRefValue +export declare function $computed( + options: WritableComputedOptions, + debuggerOptions?: DebuggerOptions +): WritableComputedRefValue diff --git a/packages/vue/package.json b/packages/vue/package.json index 8152b511571..a6a3606a909 100644 --- a/packages/vue/package.json +++ b/packages/vue/package.json @@ -34,6 +34,8 @@ }, "./dist/*": "./dist/*", "./package.json": "./package.json", + "./macros": "./macros.d.ts", + "./macros-global": "./macros-global.d.ts", "./ref-macros": "./ref-macros.d.ts" }, "buildOptions": { diff --git a/packages/vue/ref-macros.d.ts b/packages/vue/ref-macros.d.ts index c38b4bad719..6afce7f70a3 100644 --- a/packages/vue/ref-macros.d.ts +++ b/packages/vue/ref-macros.d.ts @@ -1,98 +1,2 @@ -import { - Ref, - UnwrapRef, - ComputedRef, - WritableComputedOptions, - DebuggerOptions, - WritableComputedRef -} from '@vue/runtime-dom' - -declare const RefType: unique symbol - -declare const enum RefTypes { - Ref = 1, - ComputedRef = 2, - WritableComputedRef = 3 -} - -type RefValue = T extends null | undefined - ? T - : T & { [RefType]?: RefTypes.Ref } - -type ComputedRefValue = T extends null | undefined - ? T - : T & { [RefType]?: RefTypes.ComputedRef } - -type WritableComputedRefValue = T extends null | undefined - ? T - : T & { [RefType]?: RefTypes.WritableComputedRef } - -type NormalObject = T & { [RefType]?: never } - -/** - * Vue ref transform macro for binding refs as reactive variables. - */ -declare function _$(arg: ComputedRef): ComputedRefValue -declare function _$(arg: WritableComputedRef): WritableComputedRefValue -declare function _$(arg: Ref): RefValue -declare function _$(arg?: T): DestructureRefs - -type DestructureRefs = { - [K in keyof T]: T[K] extends ComputedRef - ? ComputedRefValue - : T[K] extends WritableComputedRef - ? WritableComputedRefValue - : T[K] extends Ref - ? RefValue - : T[K] -} - -/** - * Vue ref transform macro for accessing underlying refs of reactive varaibles. - */ -declare function _$$(arg: NormalObject): ToRawRefs -declare function _$$(value: RefValue): Ref -declare function _$$(value: ComputedRefValue): ComputedRef -declare function _$$( - value: WritableComputedRefValue -): WritableComputedRef - -type ToRawRefs = { - [K in keyof T]: T[K] extends RefValue - ? Ref - : T[K] extends ComputedRefValue - ? ComputedRef - : T[K] extends WritableComputedRefValue - ? WritableComputedRef - : T[K] extends object - ? T[K] extends - | Function - | Map - | Set - | WeakMap - | WeakSet - ? T[K] - : ToRawRefs - : T[K] -} - -declare function _$ref(arg?: T | Ref): RefValue> - -declare function _$shallowRef(arg?: T): RefValue - -declare function _$computed( - getter: () => T, - debuggerOptions?: DebuggerOptions -): ComputedRefValue -declare function _$computed( - options: WritableComputedOptions, - debuggerOptions?: DebuggerOptions -): WritableComputedRefValue - -declare global { - const $: typeof _$ - const $$: typeof _$$ - const $ref: typeof _$ref - const $shallowRef: typeof _$shallowRef - const $computed: typeof _$computed -} +// TODO deprecated file - to be removed when out of experimental +import './macros-global'