Skip to content

Commit

Permalink
fix(types): UnwrapRef should bail on DOM element types.
Browse files Browse the repository at this point in the history
fix #951
  • Loading branch information
LinusBorg committed Apr 12, 2020
1 parent ab16a06 commit 828a536
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 | Element | Window | Document

// Recursively unwraps nested value bindings.
export type UnwrapRef<T> = {
Expand Down
26 changes: 24 additions & 2 deletions test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)

0 comments on commit 828a536

Please sign in to comment.