Skip to content

Commit 5e88780

Browse files
committed
chore: comment docs
1 parent df04942 commit 5e88780

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

packages/reactivity/src/ref.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ export function unref<T>(ref: MaybeRef<T>): T {
219219
return isRef(ref) ? (ref.value as any) : ref
220220
}
221221

222+
/**
223+
* Nromalizes values / refs / getters to values.
224+
* This is similar to {@link unref()}, except that it also normalizes getters.
225+
* If the argument is a getter, it will be invoked and its return value will
226+
* be returned.
227+
*
228+
* @example
229+
* ```js
230+
* toValue(1) // 1
231+
* toValue(ref(1)) // 1
232+
* toValue(() => 1) // 1
233+
* ```
234+
*
235+
* @param source - A getter, an existing ref, or a non-function value.
236+
* @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
237+
*/
222238
export function toValue<T>(source: MaybeRefOrGetter<T>): T {
223239
return isFunction(source) ? source() : unref(source)
224240
}
@@ -356,9 +372,24 @@ class GetterRefImpl<T> {
356372
export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
357373

358374
/**
359-
* Can be used to create a ref for a property on a source reactive object. The
360-
* created ref is synced with its source property: mutating the source property
361-
* will update the ref, and vice-versa.
375+
* Used to normalize values / refs / getters into refs.
376+
*
377+
* @example
378+
* ```js
379+
* // returns existing refs as-is
380+
* toRef(existingRef)
381+
*
382+
* // creates a ref that calls the getter on .value access
383+
* toRef(() => props.foo)
384+
*
385+
* // creates normal refs from non-function values
386+
* // requivalent to ref(1)
387+
* toRef(1)
388+
* ```
389+
*
390+
* Can also be used to create a ref for a property on a source reactive object.
391+
* The created ref is synced with its source property: mutating the source
392+
* property will update the ref, and vice-versa.
362393
*
363394
* @example
364395
* ```js
@@ -378,13 +409,11 @@ export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
378409
* console.log(fooRef.value) // 3
379410
* ```
380411
*
381-
* @param object - The reactive object containing the desired property.
382-
* @param key - Name of the property in the reactive object.
412+
* @param source - A getter, an existing ref, a non-function value, or a
413+
* reactive object to create a property ref from.
414+
* @param [key] - (optional) Name of the property in the reactive object.
383415
* @see {@link https://vuejs.org/api/reactivity-utilities.html#toref}
384416
*/
385-
// export function toRef<T extends () => any>(
386-
// getter: T
387-
// ): T extends () => infer R ? Readonly<Ref<R>> : never
388417
export function toRef<T>(
389418
value: T
390419
): T extends () => infer R

0 commit comments

Comments
 (0)