]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(reactivity): handle primitive + object intersection types in UnwrapRef (#614)
authorIU <yoyo930021@gmail.com>
Thu, 16 Jan 2020 22:47:47 +0000 (06:47 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 16 Jan 2020 22:47:47 +0000 (17:47 -0500)
packages/reactivity/src/ref.ts
test-dts/defineComponent.test-d.tsx

index b4127c11305dd71677b87fa4575428fe3ed571cf..10e577fe5f01ed10b4130cdfbc8fe725c38c167f 100644 (file)
@@ -85,6 +85,11 @@ function toProxyRef<T extends object, K extends keyof T>(
 
 type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
 
+// corner case when use narrows type
+// Ex. type RelativePath = string & { __brand: unknown }
+// RelativePath extends object -> true
+type BaseTypes = string | number | boolean
+
 // Recursively unwraps nested value bindings.
 export type UnwrapRef<T> = {
   cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
@@ -97,6 +102,6 @@ export type UnwrapRef<T> = {
     ? 'ref'
     : T extends Array<any>
       ? 'array'
-      : T extends Function | CollectionTypes
+      : T extends Function | CollectionTypes | BaseTypes
         ? 'ref' // bail out on types that shouldn't be unwrapped
         : T extends object ? 'object' : 'ref']
index 3cde7426e9f654e4631bd3c1f55866d40e8d909a..a6393dd15ba1974e61743435e1d51e9cbf3e1895 100644 (file)
@@ -12,6 +12,8 @@ describe('with object props', () => {
     ddd: string[]
   }
 
+  type GT = string & { __brand: unknown }
+
   const MyComponent = defineComponent({
     props: {
       a: Number,
@@ -57,6 +59,9 @@ describe('with object props', () => {
         c: ref(1),
         d: {
           e: ref('hi')
+        },
+        f: {
+          g: ref('hello' as GT)
         }
       }
     },
@@ -88,6 +93,7 @@ describe('with object props', () => {
       // assert setup context unwrapping
       expectType<number>(this.c)
       expectType<string>(this.d.e)
+      expectType<GT>(this.f.g)
 
       // setup context properties should be mutable
       this.c = 2