fix (types): do not unwrap refs in toRefs (#4966)
				
					
				
			This commit is contained in:
		
							parent
							
								
									f2d2d7b2d2
								
							
						
					
					
						commit
						c6cd6a7938
					
				@ -190,9 +190,7 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type ToRefs<T = any> = {
 | 
					export type ToRefs<T = any> = {
 | 
				
			||||||
  // #2687: somehow using ToRef<T[K]> here turns the resulting type into
 | 
					  [K in keyof T]: ToRef<T[K]>
 | 
				
			||||||
  // a union of multiple Ref<*> types instead of a single Ref<* | *> type.
 | 
					 | 
				
			||||||
  [K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
export function toRefs<T extends object>(object: T): ToRefs<T> {
 | 
					export function toRefs<T extends object>(object: T): ToRefs<T> {
 | 
				
			||||||
  if (__DEV__ && !isProxy(object)) {
 | 
					  if (__DEV__ && !isProxy(object)) {
 | 
				
			||||||
 | 
				
			|||||||
@ -10,8 +10,7 @@ import {
 | 
				
			|||||||
  toRef,
 | 
					  toRef,
 | 
				
			||||||
  toRefs,
 | 
					  toRefs,
 | 
				
			||||||
  ToRefs,
 | 
					  ToRefs,
 | 
				
			||||||
  shallowReactive,
 | 
					  shallowReactive
 | 
				
			||||||
  watch
 | 
					 | 
				
			||||||
} from './index'
 | 
					} from './index'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function plainType(arg: number | Ref<number>) {
 | 
					function plainType(arg: number | Ref<number>) {
 | 
				
			||||||
@ -185,28 +184,45 @@ const p2 = proxyRefs(r2)
 | 
				
			|||||||
expectType<number>(p2.a)
 | 
					expectType<number>(p2.a)
 | 
				
			||||||
expectType<Ref<string>>(p2.obj.k)
 | 
					expectType<Ref<string>>(p2.obj.k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// toRef
 | 
					// toRef and toRefs
 | 
				
			||||||
const obj = {
 | 
					{
 | 
				
			||||||
 | 
					  const obj: {
 | 
				
			||||||
 | 
					    a: number
 | 
				
			||||||
 | 
					    b: Ref<number>
 | 
				
			||||||
 | 
					    c: number | string
 | 
				
			||||||
 | 
					  } = {
 | 
				
			||||||
    a: 1,
 | 
					    a: 1,
 | 
				
			||||||
  b: ref(1)
 | 
					    b: ref(1),
 | 
				
			||||||
 | 
					    c: 1
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // toRef
 | 
				
			||||||
  expectType<Ref<number>>(toRef(obj, 'a'))
 | 
					  expectType<Ref<number>>(toRef(obj, 'a'))
 | 
				
			||||||
  expectType<Ref<number>>(toRef(obj, 'b'))
 | 
					  expectType<Ref<number>>(toRef(obj, 'b'))
 | 
				
			||||||
 | 
					  // Should not distribute Refs over union
 | 
				
			||||||
const objWithUnionProp: { a: string | number } = {
 | 
					  expectType<Ref<number | string>>(toRef(obj, 'c'))
 | 
				
			||||||
  a: 1
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
watch(toRef(objWithUnionProp, 'a'), value => {
 | 
					 | 
				
			||||||
  expectType<string | number>(value)
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // toRefs
 | 
					  // toRefs
 | 
				
			||||||
const objRefs = toRefs(obj)
 | 
					 | 
				
			||||||
  expectType<{
 | 
					  expectType<{
 | 
				
			||||||
    a: Ref<number>
 | 
					    a: Ref<number>
 | 
				
			||||||
    b: Ref<number>
 | 
					    b: Ref<number>
 | 
				
			||||||
}>(objRefs)
 | 
					    // Should not distribute Refs over union
 | 
				
			||||||
 | 
					    c: Ref<number | string>
 | 
				
			||||||
 | 
					  }>(toRefs(obj))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Both should not do any unwrapping
 | 
				
			||||||
 | 
					  const someReactive = shallowReactive({
 | 
				
			||||||
 | 
					    a: {
 | 
				
			||||||
 | 
					      b: ref(42)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const toRefResult = toRef(someReactive, 'a')
 | 
				
			||||||
 | 
					  const toRefsResult = toRefs(someReactive)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  expectType<Ref<number>>(toRefResult.value.b)
 | 
				
			||||||
 | 
					  expectType<Ref<number>>(toRefsResult.a.value.b)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// #2687
 | 
					// #2687
 | 
				
			||||||
interface AppData {
 | 
					interface AppData {
 | 
				
			||||||
@ -238,20 +254,6 @@ function testUnrefGenerics<T>(p: T | Ref<T>) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
testUnrefGenerics(1)
 | 
					testUnrefGenerics(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// #4732
 | 
					 | 
				
			||||||
describe('ref in shallow reactive', () => {
 | 
					 | 
				
			||||||
  const baz = shallowReactive({
 | 
					 | 
				
			||||||
    foo: {
 | 
					 | 
				
			||||||
      bar: ref(42)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const foo = toRef(baz, 'foo')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  expectType<Ref<number>>(foo.value.bar)
 | 
					 | 
				
			||||||
  expectType<number>(foo.value.bar.value)
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// #4771
 | 
					// #4771
 | 
				
			||||||
describe('shallow reactive in reactive', () => {
 | 
					describe('shallow reactive in reactive', () => {
 | 
				
			||||||
  const baz = reactive({
 | 
					  const baz = reactive({
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user