From 33ccfc0a8b69de13065c4b995f88722dd72a1ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Mon, 13 Apr 2020 17:51:32 +0200 Subject: [PATCH] fix(types): UnwrapRef should bail on DOM element types (#952) fix #951 --- packages/reactivity/src/ref.ts | 2 +- test-dts/ref.test-d.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 3b334538c11..05304ad7ef9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -101,7 +101,7 @@ function toProxyRef( // 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 = { diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index d4f07253887..4f862a0686d 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -1,7 +1,7 @@ import { expectType } from 'tsd' import { Ref, ref, isRef, unref } from './index' -function foo(arg: number | Ref) { +function plainType(arg: number | Ref) { // ref coercing const coerced = ref(arg) expectType>(coerced) @@ -22,4 +22,26 @@ function foo(arg: number | Ref) { expectType<{ foo: number }>(nestedRef.value) } -foo(1) +plainType(1) + +function bailType(arg: HTMLElement | Ref) { + // ref coercing + const coerced = ref(arg) + expectType>(coerced) + + // isRef as type guard + if (isRef(arg)) { + expectType>(arg) + } + + // ref unwrapping + expectType(unref(arg)) + + // ref inner type should be unwrapped + const nestedRef = ref({ foo: ref(document.createElement('DIV')) }) + + expectType>(nestedRef) + expectType<{ foo: HTMLElement }>(nestedRef.value) +} +const el = document.createElement('DIV') +bailType(el)