2020-02-22 00:45:42 +08:00
|
|
|
import { expectType } from 'tsd'
|
2020-02-22 11:39:32 +08:00
|
|
|
import { Ref, ref, isRef, unref } from './index'
|
2020-02-22 00:45:42 +08:00
|
|
|
|
2020-04-13 23:51:32 +08:00
|
|
|
function plainType(arg: number | Ref<number>) {
|
2020-02-22 00:45:42 +08:00
|
|
|
// ref coercing
|
|
|
|
const coerced = ref(arg)
|
|
|
|
expectType<Ref<number>>(coerced)
|
|
|
|
|
|
|
|
// isRef as type guard
|
|
|
|
if (isRef(arg)) {
|
|
|
|
expectType<Ref<number>>(arg)
|
|
|
|
}
|
2020-02-22 11:39:32 +08:00
|
|
|
|
|
|
|
// ref unwrapping
|
|
|
|
expectType<number>(unref(arg))
|
2020-02-26 08:44:06 +08:00
|
|
|
|
|
|
|
// ref inner type should be unwrapped
|
|
|
|
const nestedRef = ref({
|
|
|
|
foo: ref(1)
|
|
|
|
})
|
|
|
|
expectType<Ref<{ foo: number }>>(nestedRef)
|
2020-02-26 09:38:02 +08:00
|
|
|
expectType<{ foo: number }>(nestedRef.value)
|
2020-02-22 00:45:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 23:51:32 +08:00
|
|
|
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)
|