fix(types): UnwrapRef should bail on DOM element types (#952)

fix #951
This commit is contained in:
Thorsten Lünborg 2020-04-13 17:51:32 +02:00 committed by GitHub
parent 5968cff3a8
commit 33ccfc0a8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -101,7 +101,7 @@ function toProxyRef<T extends object, K extends keyof T>(
// corner case when use narrows type // corner case when use narrows type
// Ex. type RelativePath = string & { __brand: unknown } // Ex. type RelativePath = string & { __brand: unknown }
// RelativePath extends object -> true // RelativePath extends object -> true
type BaseTypes = string | number | boolean type BaseTypes = string | number | boolean | Node | Window
// Recursively unwraps nested value bindings. // Recursively unwraps nested value bindings.
export type UnwrapRef<T> = { export type UnwrapRef<T> = {

View File

@ -1,7 +1,7 @@
import { expectType } from 'tsd' import { expectType } from 'tsd'
import { Ref, ref, isRef, unref } from './index' import { Ref, ref, isRef, unref } from './index'
function foo(arg: number | Ref<number>) { function plainType(arg: number | Ref<number>) {
// ref coercing // ref coercing
const coerced = ref(arg) const coerced = ref(arg)
expectType<Ref<number>>(coerced) expectType<Ref<number>>(coerced)
@ -22,4 +22,26 @@ function foo(arg: number | Ref<number>) {
expectType<{ foo: number }>(nestedRef.value) 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)