]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): UnwrapRef should bail on DOM element types (#952)
authorThorsten Lünborg <t.luenborg@googlemail.com>
Mon, 13 Apr 2020 15:51:32 +0000 (17:51 +0200)
committerGitHub <noreply@github.com>
Mon, 13 Apr 2020 15:51:32 +0000 (11:51 -0400)
fix #951

packages/reactivity/src/ref.ts
test-dts/ref.test-d.ts

index 3b334538c11ac6c57b0063352c7f42cf472ae549..05304ad7ef90384bfc954c840d7a869d6fd67fc7 100644 (file)
@@ -101,7 +101,7 @@ function toProxyRef<T extends object, K extends keyof T>(
 // corner case when use narrows type
 // Ex. type RelativePath = string & { __brand: unknown }
 // RelativePath extends object -> true
-type BaseTypes = string | number | boolean
+type BaseTypes = string | number | boolean | Node | Window
 
 // Recursively unwraps nested value bindings.
 export type UnwrapRef<T> = {
index d4f0725388740bc46f22220ae945430fcd908dcb..4f862a0686dbc6b9227ca0f4cac01a0a209e82f8 100644 (file)
@@ -1,7 +1,7 @@
 import { expectType } from 'tsd'
 import { Ref, ref, isRef, unref } from './index'
 
-function foo(arg: number | Ref<number>) {
+function plainType(arg: number | Ref<number>) {
   // ref coercing
   const coerced = ref(arg)
   expectType<Ref<number>>(coerced)
@@ -22,4 +22,26 @@ function foo(arg: number | Ref<number>) {
   expectType<{ foo: number }>(nestedRef.value)
 }
 
-foo(1)
+plainType(1)
+
+function bailType(arg: HTMLElement | Ref<HTMLElement>) {
+  // ref coercing
+  const coerced = ref(arg)
+  expectType<Ref<HTMLElement>>(coerced)
+
+  // isRef as type guard
+  if (isRef(arg)) {
+    expectType<Ref<HTMLElement>>(arg)
+  }
+
+  // ref unwrapping
+  expectType<HTMLElement>(unref(arg))
+
+  // ref inner type should be unwrapped
+  const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
+
+  expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
+  expectType<{ foo: HTMLElement }>(nestedRef.value)
+}
+const el = document.createElement('DIV')
+bailType(el)