diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index e47b8aa5582..784acc575f8 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -8,10 +8,12 @@ import { import { DirtyLevels, TrackOpTypes, TriggerOpTypes } from './constants' import { type IfAny, + type Ref, hasChanged, isArray, isFunction, isObject, + isRef, } from '@vue/shared' import { isProxy, @@ -27,19 +29,9 @@ import { ComputedRefImpl } from './computed' import { getDepFromReactive } from './reactiveEffect' import { warn } from './warning' -declare const RefSymbol: unique symbol +export type { Ref } from '@vue/shared' export declare const RawSymbol: unique symbol -export interface Ref { - value: T - /** - * Type differentiator only. - * We need this to be in public d.ts but don't want it to show up in IDE - * autocomplete, so we use a private Symbol instead. - */ - [RefSymbol]: true -} - type RefBase = { dep?: Dep value: T @@ -90,16 +82,7 @@ export function triggerRefValue( } } -/** - * Checks if a value is a ref object. - * - * @param r - The value to inspect. - * @see {@link https://vuejs.org/api/reactivity-utilities.html#isref} - */ -export function isRef(r: Ref | unknown): r is Ref -export function isRef(r: any): r is Ref { - return !!(r && r.__v_isRef === true) -} +export { isRef } from '@vue/shared' /** * Takes an inner value and returns a reactive and mutable ref object, which diff --git a/packages/shared/src/general.ts b/packages/shared/src/general.ts index d2add125502..0515257f7a0 100644 --- a/packages/shared/src/general.ts +++ b/packages/shared/src/general.ts @@ -1,4 +1,5 @@ import { makeMap } from './makeMap' +import type { Ref } from './typeUtils' export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__ ? Object.freeze({}) @@ -52,6 +53,20 @@ export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol' export const isObject = (val: unknown): val is Record => val !== null && typeof val === 'object' +// @vue/shared can't have dependencies, +// but this is needed here for #7306 + +/** + * Checks if a value is a ref object. + * + * @param r - The value to inspect. + * @see {@link https://vuejs.org/api/reactivity-utilities.html#isref} + */ +export function isRef(r: Ref | unknown): r is Ref +export function isRef(r: any): r is Ref { + return !!(r && r.__v_isRef === true) +} + export const isPromise = (val: unknown): val is Promise => { return ( (isObject(val) || isFunction(val)) && diff --git a/packages/shared/src/toDisplayString.ts b/packages/shared/src/toDisplayString.ts index 6d6948bc5d2..c46d28bf696 100644 --- a/packages/shared/src/toDisplayString.ts +++ b/packages/shared/src/toDisplayString.ts @@ -4,17 +4,13 @@ import { isMap, isObject, isPlainObject, + isRef, isSet, isString, isSymbol, objectToString, } from './general' -// can't use isRef here since @vue/shared has no deps -const isRef = (val: any): val is { value: unknown } => { - return !!(val && val.__v_isRef === true) -} - /** * For converting {{ interpolation }} values to displayed strings. * @private diff --git a/packages/shared/src/typeUtils.ts b/packages/shared/src/typeUtils.ts index 63372d82916..d107c8192f0 100644 --- a/packages/shared/src/typeUtils.ts +++ b/packages/shared/src/typeUtils.ts @@ -21,3 +21,15 @@ export type Awaited = T extends null | undefined ? Awaited // recursively unwrap the value : never // the argument to `then` was not callable : T // non-object or non-thenable + +declare const RefSymbol: unique symbol + +export interface Ref { + value: T + /** + * Type differentiator only. + * We need this to be in public d.ts but don't want it to show up in IDE + * autocomplete, so we use a private Symbol instead. + */ + [RefSymbol]: true +}