R
fn

extendRef

v0.0.15testeddemo

Attach extra (optionally reactive) attributes to a ref while keeping it a usable ref.

Examples

ts
const myRef = ref('content');
const extended = extendRef(myRef, { foo: 'bar' });
extended.value; // 'content'
extended.foo; // 'bar'
ts
// reactive extension: unwrapped two-way by default
const count = ref(0);
const extended = extendRef(count, { double: computed(() => count.value * 2) });
extended.double; // 0 (no .value needed)
ts
// keep refs as refs with unwrap: false
const extended = extendRef(ref(0), { inner: ref(1) }, { unwrap: false });
extended.inner.value; // 1

Demo

Loading demo…

Signatures

ts
export function extendRef<R extends Ref<unknown>, Extend extends object, Options extends ExtendRefOptions<false>>(ref: R, extend: Extend, options: Options): R & ShallowUnwrapRef<Extend>;
ts
export function extendRef<R extends Ref<unknown>, Extend extends object, Options extends ExtendRefOptions>(ref: R, extend: Extend, options?: Options): R & Extend;

Type Parameters

Rextends Ref<unknown>
Extendextends object

Parameters

ParameterTypeDescription
refRThe ref to extend
extendExtendThe ref to extend
options?ExtendRefOptions<boolean>`enumerable` (default `false`) and `unwrap` (default `true`)

Returns

R & ExtendThe same ref instance, now carrying the extended properties