fn

set

v0.0.14testeddemo

Shorthand setter that mirrors get. Either assigns value to a ref (ref.value = value) or assigns value to a single property of an object (target[key] = value). The arity is resolved at the type level via overloads, so both forms stay fully type-safe. Purely synchronous and side-effect free, so it is fully SSR-safe.

Examples

ts
const count = ref(0);
set(count, 5); // count.value === 5
ts
const user = reactive({ name: 'Ada' });
set(user, 'name', 'Grace'); // user.name === 'Grace'

Demo

Loading demo…

Signatures

ts
export function set<T>(ref: Ref<T>, value: T): void;
ts
export function set<O extends object, K extends keyof O>(target: O, key: K, value: O[K]): void;

Type Parameters

Oextends object
Kextends keyof O

Parameters

ParameterTypeDescription
targetRef<unknown> | OThe ref to write to, or the object to mutate
valueOrKeyK | unknownThe value to assign to the ref, or the key to mutate
value?O[K]The value to assign to the ref, or the key to mutate