* fix(types): make `toRef` return correct type(fix #4732) * chore: use correct test Co-authored-by: Evan You <yyx990803@gmail.com>
This commit is contained in:
		
							parent
							
								
									f66d456b7a
								
							
						
					
					
						commit
						925bc346fe
					
				@ -65,7 +65,9 @@ export function isRef(r: any): r is Ref {
 | 
				
			|||||||
  return Boolean(r && r.__v_isRef === true)
 | 
					  return Boolean(r && r.__v_isRef === true)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function ref<T extends object>(value: T): ToRef<T>
 | 
					export function ref<T extends object>(
 | 
				
			||||||
 | 
					  value: T
 | 
				
			||||||
 | 
					): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
 | 
				
			||||||
export function ref<T>(value: T): Ref<UnwrapRef<T>>
 | 
					export function ref<T>(value: T): Ref<UnwrapRef<T>>
 | 
				
			||||||
export function ref<T = any>(): Ref<T | undefined>
 | 
					export function ref<T = any>(): Ref<T | undefined>
 | 
				
			||||||
export function ref(value?: unknown) {
 | 
					export function ref(value?: unknown) {
 | 
				
			||||||
@ -212,7 +214,7 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
 | 
					export type ToRef<T> = [T] extends [Ref] ? T : Ref<T>
 | 
				
			||||||
export function toRef<T extends object, K extends keyof T>(
 | 
					export function toRef<T extends object, K extends keyof T>(
 | 
				
			||||||
  object: T,
 | 
					  object: T,
 | 
				
			||||||
  key: K
 | 
					  key: K
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,7 @@ import {
 | 
				
			|||||||
  toRef,
 | 
					  toRef,
 | 
				
			||||||
  toRefs,
 | 
					  toRefs,
 | 
				
			||||||
  ToRefs,
 | 
					  ToRefs,
 | 
				
			||||||
 | 
					  shallowReactive,
 | 
				
			||||||
  watch
 | 
					  watch
 | 
				
			||||||
} from './index'
 | 
					} from './index'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -236,3 +237,15 @@ function testUnrefGenerics<T>(p: T | Ref<T>) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
testUnrefGenerics(1)
 | 
					testUnrefGenerics(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// #4732
 | 
				
			||||||
 | 
					const baz = shallowReactive({
 | 
				
			||||||
 | 
					  foo: {
 | 
				
			||||||
 | 
					    bar: ref(42)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const foo = toRef(baz, 'foo')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					expectType<Ref<number>>(foo.value.bar)
 | 
				
			||||||
 | 
					expectType<number>(foo.value.bar.value)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user