@@ -219,6 +219,22 @@ export function unref<T>(ref: MaybeRef<T>): T {
219
219
return isRef ( ref ) ? ( ref . value as any ) : ref
220
220
}
221
221
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
+ */
222
238
export function toValue < T > ( source : MaybeRefOrGetter < T > ) : T {
223
239
return isFunction ( source ) ? source ( ) : unref ( source )
224
240
}
@@ -356,9 +372,24 @@ class GetterRefImpl<T> {
356
372
export type ToRef < T > = IfAny < T , Ref < T > , [ T ] extends [ Ref ] ? T : Ref < T > >
357
373
358
374
/**
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.
362
393
*
363
394
* @example
364
395
* ```js
@@ -378,13 +409,11 @@ export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
378
409
* console.log(fooRef.value) // 3
379
410
* ```
380
411
*
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.
383
415
* @see {@link https://vuejs.org/api/reactivity-utilities.html#toref }
384
416
*/
385
- // export function toRef<T extends () => any>(
386
- // getter: T
387
- // ): T extends () => infer R ? Readonly<Ref<R>> : never
388
417
export function toRef < T > (
389
418
value : T
390
419
) : T extends ( ) => infer R
0 commit comments