Skip to content

Commit 33ccfc0

Browse files
authored
fix(types): UnwrapRef should bail on DOM element types (#952)
fix #951
1 parent 5968cff commit 33ccfc0

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/reactivity/src/ref.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function toProxyRef<T extends object, K extends keyof T>(
101101
// corner case when use narrows type
102102
// Ex. type RelativePath = string & { __brand: unknown }
103103
// RelativePath extends object -> true
104-
type BaseTypes = string | number | boolean
104+
type BaseTypes = string | number | boolean | Node | Window
105105

106106
// Recursively unwraps nested value bindings.
107107
export type UnwrapRef<T> = {

test-dts/ref.test-d.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expectType } from 'tsd'
22
import { Ref, ref, isRef, unref } from './index'
33

4-
function foo(arg: number | Ref<number>) {
4+
function plainType(arg: number | Ref<number>) {
55
// ref coercing
66
const coerced = ref(arg)
77
expectType<Ref<number>>(coerced)
@@ -22,4 +22,26 @@ function foo(arg: number | Ref<number>) {
2222
expectType<{ foo: number }>(nestedRef.value)
2323
}
2424

25-
foo(1)
25+
plainType(1)
26+
27+
function bailType(arg: HTMLElement | Ref<HTMLElement>) {
28+
// ref coercing
29+
const coerced = ref(arg)
30+
expectType<Ref<HTMLElement>>(coerced)
31+
32+
// isRef as type guard
33+
if (isRef(arg)) {
34+
expectType<Ref<HTMLElement>>(arg)
35+
}
36+
37+
// ref unwrapping
38+
expectType<HTMLElement>(unref(arg))
39+
40+
// ref inner type should be unwrapped
41+
const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
42+
43+
expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
44+
expectType<{ foo: HTMLElement }>(nestedRef.value)
45+
}
46+
const el = document.createElement('DIV')
47+
bailType(el)

0 commit comments

Comments
 (0)