@@ -7,10 +7,15 @@ import {
7
7
reactive ,
8
8
proxyRefs ,
9
9
toRef ,
10
+ toValue ,
10
11
toRefs ,
11
12
ToRefs ,
12
13
shallowReactive ,
13
- readonly
14
+ readonly ,
15
+ MaybeRef ,
16
+ MaybeRefOrGetter ,
17
+ ComputedRef ,
18
+ computed
14
19
} from 'vue'
15
20
import { expectType , describe } from './utils'
16
21
@@ -26,6 +31,8 @@ function plainType(arg: number | Ref<number>) {
26
31
27
32
// ref unwrapping
28
33
expectType < number > ( unref ( arg ) )
34
+ expectType < number > ( toValue ( arg ) )
35
+ expectType < number > ( toValue ( ( ) => 123 ) )
29
36
30
37
// ref inner type should be unwrapped
31
38
const nestedRef = ref ( {
@@ -203,6 +210,13 @@ expectType<Ref<string>>(p2.obj.k)
203
210
// Should not distribute Refs over union
204
211
expectType < Ref < number | string > > ( toRef ( obj , 'c' ) )
205
212
213
+ expectType < Ref < number > > ( toRef ( ( ) => 123 ) )
214
+ expectType < Ref < number | string > > ( toRef ( ( ) => obj . c ) )
215
+
216
+ const r = toRef ( ( ) => 123 )
217
+ // @ts -expect-error
218
+ r . value = 234
219
+
206
220
// toRefs
207
221
expectType < {
208
222
a : Ref < number >
@@ -319,3 +333,58 @@ describe('reactive in shallow ref', () => {
319
333
320
334
expectType < number > ( x . value . a . b )
321
335
} )
336
+
337
+ describe ( 'toRef <-> toValue' , ( ) => {
338
+ function foo (
339
+ a : MaybeRef < string > ,
340
+ b : ( ) => string ,
341
+ c : MaybeRefOrGetter < string > ,
342
+ d : ComputedRef < string >
343
+ ) {
344
+ const r = toRef ( a )
345
+ expectType < Ref < string > > ( r )
346
+ // writable
347
+ r . value = 'foo'
348
+
349
+ const rb = toRef ( b )
350
+ expectType < Readonly < Ref < string > > > ( rb )
351
+ // @ts -expect-error ref created from getter should be readonly
352
+ rb . value = 'foo'
353
+
354
+ const rc = toRef ( c )
355
+ expectType < Readonly < Ref < string > | Ref < string > > > ( rc )
356
+ // @ts -expect-error ref created from MaybeReadonlyRef should be readonly
357
+ rc . value = 'foo'
358
+
359
+ const rd = toRef ( d )
360
+ expectType < ComputedRef < string > > ( rd )
361
+ // @ts -expect-error ref created from computed ref should be readonly
362
+ rd . value = 'foo'
363
+
364
+ expectType < string > ( toValue ( a ) )
365
+ expectType < string > ( toValue ( b ) )
366
+ expectType < string > ( toValue ( c ) )
367
+ expectType < string > ( toValue ( d ) )
368
+
369
+ return {
370
+ r : toValue ( r ) ,
371
+ rb : toValue ( rb ) ,
372
+ rc : toValue ( rc ) ,
373
+ rd : toValue ( rd )
374
+ }
375
+ }
376
+
377
+ expectType < {
378
+ r : string
379
+ rb : string
380
+ rc : string
381
+ rd : string
382
+ } > (
383
+ foo (
384
+ 'foo' ,
385
+ ( ) => 'bar' ,
386
+ ref ( 'baz' ) ,
387
+ computed ( ( ) => 'hi' )
388
+ )
389
+ )
390
+ } )
0 commit comments