57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { expectType } from 'tsd'
|
|
import { Ref, ref, isRef, unref, UnwrapRef } from './index'
|
|
|
|
function plainType(arg: number | Ref<number>) {
|
|
// ref coercing
|
|
const coerced = ref(arg)
|
|
expectType<Ref<number>>(coerced)
|
|
|
|
// isRef as type guard
|
|
if (isRef(arg)) {
|
|
expectType<Ref<number>>(arg)
|
|
}
|
|
|
|
// ref unwrapping
|
|
expectType<number>(unref(arg))
|
|
|
|
// ref inner type should be unwrapped
|
|
const nestedRef = ref({
|
|
foo: ref(1)
|
|
})
|
|
expectType<Ref<{ foo: number }>>(nestedRef)
|
|
expectType<{ foo: number }>(nestedRef.value)
|
|
|
|
interface IteratorFoo {
|
|
[Symbol.iterator]: any
|
|
}
|
|
expectType<Ref<UnwrapRef<IteratorFoo>> | Ref<null>>(
|
|
ref<IteratorFoo | null>(null)
|
|
)
|
|
|
|
expectType<Ref<HTMLElement> | Ref<null>>(ref<HTMLElement | null>(null))
|
|
}
|
|
|
|
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)
|