diff --git a/packages/runtime-core/src/helpers/refSugar.ts b/packages/runtime-core/src/helpers/refSugar.ts deleted file mode 100644 index 45d0c399e2c..00000000000 --- a/packages/runtime-core/src/helpers/refSugar.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { - Ref, - UnwrapRef, - ShallowUnwrapRef, - ComputedRef, - WritableComputedOptions, - DebuggerOptions, - WritableComputedRef -} from '@vue/reactivity' - -declare const RefMarker: unique symbol -type RefValue = T & { [RefMarker]?: any } - -export function $ref(arg?: T | Ref): RefValue> -export function $ref() {} - -export function $shallowRef(arg?: T): RefValue -export function $shallowRef() {} - -declare const ComputedRefMarker: unique symbol -type ComputedRefValue = T & { [ComputedRefMarker]?: any } - -declare const WritableComputedRefMarker: unique symbol -type WritableComputedRefValue = T & { [WritableComputedRefMarker]?: any } - -export function $computed( - getter: () => T, - debuggerOptions?: DebuggerOptions -): ComputedRefValue -export function $computed( - options: WritableComputedOptions, - debuggerOptions?: DebuggerOptions -): WritableComputedRefValue -export function $computed() {} - -export function $fromRefs(source: T): ShallowUnwrapRef -export function $fromRefs() { - return null as any -} - -export function $raw>( - value: T -): T extends ComputedRefValue ? ComputedRef : never -export function $raw( - value: WritableComputedRefValue -): WritableComputedRef -export function $raw(value: RefValue): Ref -export function $raw() { - return null as any -} diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index c46b6df2276..0ba1d9c2fa9 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -149,6 +149,7 @@ declare module '@vue/reactivity' { export { ReactiveEffectOptions, DebuggerEvent, + DebuggerOptions, TrackOpTypes, TriggerOpTypes, Ref, @@ -351,13 +352,3 @@ const _compatUtils = { export const compatUtils = ( __COMPAT__ ? _compatUtils : null ) as typeof _compatUtils - -// Ref sugar macros ------------------------------------------------------------ -// for dts generation only -export { - $ref, - $shallowRef, - $computed, - $raw, - $fromRefs -} from './helpers/refSugar' diff --git a/packages/runtime-core/types/scriptSetupHelpers.d.ts b/packages/runtime-core/types/scriptSetupHelpers.d.ts index 69cab07ada3..4d168212c54 100644 --- a/packages/runtime-core/types/scriptSetupHelpers.d.ts +++ b/packages/runtime-core/types/scriptSetupHelpers.d.ts @@ -5,21 +5,9 @@ type _defineEmits = typeof defineEmits type _defineExpose = typeof defineExpose type _withDefaults = typeof withDefaults -type _ref = typeof $ref -type _shallowRef = typeof $shallowRef -type _computed = typeof $computed -type _fromRefs = typeof $fromRefs -type _raw = typeof $raw - declare global { const defineProps: _defineProps const defineEmits: _defineEmits const defineExpose: _defineExpose const withDefaults: _withDefaults - - const $ref: _ref - const $shallowRef: _shallowRef - const $computed: _computed - const $fromRefs: _fromRefs - const $raw: _raw } diff --git a/packages/vue/ref-macros.d.ts b/packages/vue/ref-macros.d.ts new file mode 100644 index 00000000000..547a74879a7 --- /dev/null +++ b/packages/vue/ref-macros.d.ts @@ -0,0 +1,76 @@ +import { + Ref, + UnwrapRef, + ComputedRef, + WritableComputedOptions, + DebuggerOptions, + WritableComputedRef, + ShallowUnwrapRef +} from '@vue/runtime-dom' + +declare const RefMarker: unique symbol +type RefValue = T & { [RefMarker]?: any } + +declare const ComputedRefMarker: unique symbol +type ComputedRefValue = T & { [ComputedRefMarker]?: any } + +declare const WritableComputedRefMarker: unique symbol +type WritableComputedRefValue = T & { [WritableComputedRefMarker]?: any } + +/** + * 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): ShallowUnwrapRef + +/** + * Vue ref transform macro for accessing underlying refs of reactive varaibles. + */ +declare function _$$(value: T): ComputedRef +declare function _$$( + value: WritableComputedRefValue +): WritableComputedRef +declare function _$$(value: RefValue): Ref +declare function _$$(arg: T): ToRawRefs + +type ToRawRefs = { + [K in keyof T]: T[K] extends ComputedRefValue + ? ComputedRefValue + : T[K] extends WritableComputedRefValue + ? WritableComputedRef + : T[K] extends RefValue + ? Ref + : 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 +} diff --git a/test-dts/refSugar.test-d.ts b/test-dts/refTransformMacros.test-d.ts similarity index 72% rename from test-dts/refSugar.test-d.ts rename to test-dts/refTransformMacros.test-d.ts index 3f8c670d9eb..dfabb55fb5e 100644 --- a/test-dts/refSugar.test-d.ts +++ b/test-dts/refTransformMacros.test-d.ts @@ -1,15 +1,17 @@ import { WritableComputedRef } from '@vue/reactivity' -import { - expectType, - $ref, - $shallowRef, - $computed, - $fromRefs, - $raw, - ref, - Ref, - ComputedRef -} from './index' +import { expectType, ref, Ref, ComputedRef } from './index' +import 'vue/ref-macros' + +// wrapping refs +// normal +// computed +// writable computed + +// destructure +const { x, y, z } = $(useFoo()) +expectType(x) +expectType(y) +expectType(z) // $ref expectType($ref(1)) @@ -45,23 +47,19 @@ function useFoo() { } } -// $fromRefs -const { x, y, z } = $fromRefs(useFoo()) -expectType(x) -expectType(y) -expectType(z) - -// $raw -expectType>($raw(x)) -expectType>($raw(y)) +// $$ +expectType>($$(x)) +expectType>($$(y)) const c = $computed(() => 1) -const cRef = $raw(c) +const cRef = $$(c) expectType>(cRef) const c2 = $computed({ get: () => 1, set: () => {} }) -const c2Ref = $raw(c2) +const c2Ref = $$(c2) expectType>(c2Ref) + +// $$ on object