@@ -59,6 +59,28 @@ function getTargetType(value: Target) {
59
59
// only unwrap nested ref
60
60
type UnwrapNestedRefs < T > = T extends Ref ? T : UnwrapRef < T >
61
61
62
+ /**
63
+ * Creates a reactive copy of the original object.
64
+ *
65
+ * The reactive conversion is "deep"—it affects all nested properties. In the
66
+ * ES2015 Proxy based implementation, the returned proxy is **not** equal to the
67
+ * original object. It is recommended to work exclusively with the reactive
68
+ * proxy and avoid relying on the original object.
69
+ *
70
+ * A reactive object also automatically unwraps refs contained in it, so you
71
+ * don't need to use `.value` when accessing and mutating their value:
72
+ *
73
+ * ```js
74
+ * const count = ref(0)
75
+ * const obj = reactive({
76
+ * count
77
+ * })
78
+ *
79
+ * obj.count++
80
+ * obj.count // -> 1
81
+ * count.value // -> 1
82
+ * ```
83
+ */
62
84
export function reactive < T extends object > ( target : T ) : UnwrapNestedRefs < T >
63
85
export function reactive ( target : object ) {
64
86
// if trying to observe a readonly proxy, return the readonly version.
@@ -73,9 +95,11 @@ export function reactive(target: object) {
73
95
)
74
96
}
75
97
76
- // Return a reactive-copy of the original object, where only the root level
77
- // properties are reactive, and does NOT unwrap refs nor recursively convert
78
- // returned properties.
98
+ /**
99
+ * Return a shallowly-reactive copy of the original object, where only the root
100
+ * level properties are reactive. It also does not auto-unwrap refs (even at the
101
+ * root level).
102
+ */
79
103
export function shallowReactive < T extends object > ( target : T ) : T {
80
104
return createReactiveObject (
81
105
target ,
@@ -107,6 +131,10 @@ export type DeepReadonly<T> = T extends Builtin
107
131
? { readonly [ K in keyof T ] : DeepReadonly < T [ K ] > }
108
132
: Readonly < T >
109
133
134
+ /**
135
+ * Creates a readonly copy of the original object. Note the returned copy is not
136
+ * made reactive, but `readonly` can be called on an already reactive object.
137
+ */
110
138
export function readonly < T extends object > (
111
139
target : T
112
140
) : DeepReadonly < UnwrapNestedRefs < T > > {
@@ -118,10 +146,12 @@ export function readonly<T extends object>(
118
146
)
119
147
}
120
148
121
- // Return a reactive-copy of the original object, where only the root level
122
- // properties are readonly, and does NOT unwrap refs nor recursively convert
123
- // returned properties.
124
- // This is used for creating the props proxy object for stateful components.
149
+ /**
150
+ * Returns a reactive-copy of the original object, where only the root level
151
+ * properties are readonly, and does NOT unwrap refs nor recursively convert
152
+ * returned properties.
153
+ * This is used for creating the props proxy object for stateful components.
154
+ */
125
155
export function shallowReadonly < T extends object > (
126
156
target : T
127
157
) : Readonly < { [ K in keyof T ] : UnwrapNestedRefs < T [ K ] > } > {
0 commit comments