2020-06-09 22:17:42 +08:00
|
|
|
import { Ref, ref, isRef, unref, reactive, expectType } 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-04-12 18:25:46 +08:00
|
|
|
|
2020-04-22 23:54:54 +08:00
|
|
|
// ref boolean
|
|
|
|
const falseRef = ref(false)
|
|
|
|
expectType<Ref<boolean>>(falseRef)
|
|
|
|
expectType<boolean>(falseRef.value)
|
|
|
|
|
|
|
|
// ref true
|
|
|
|
const trueRef = ref<true>(true)
|
|
|
|
expectType<Ref<true>>(trueRef)
|
|
|
|
expectType<true>(trueRef.value)
|
|
|
|
|
2020-04-15 23:22:44 +08:00
|
|
|
// tuple
|
|
|
|
expectType<[number, string]>(unref(ref([1, '1'])))
|
|
|
|
|
2020-04-12 18:25:46 +08:00
|
|
|
interface IteratorFoo {
|
|
|
|
[Symbol.iterator]: any
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:22:44 +08:00
|
|
|
// with symbol
|
2020-04-16 00:15:13 +08:00
|
|
|
expectType<Ref<IteratorFoo | null | undefined>>(
|
|
|
|
ref<IteratorFoo | null | undefined>()
|
|
|
|
)
|
2020-06-30 00:26:28 +08:00
|
|
|
|
|
|
|
// should not unwrap ref inside arrays
|
|
|
|
const arr = ref([1, new Map<string, any>(), ref('1')]).value
|
|
|
|
const value = arr[0]
|
|
|
|
if (isRef(value)) {
|
|
|
|
expectType<Ref>(value)
|
|
|
|
} else if (typeof value === 'number') {
|
|
|
|
expectType<number>(value)
|
|
|
|
} else {
|
|
|
|
// should narrow down to Map type
|
|
|
|
// and not contain any Ref type
|
|
|
|
expectType<Map<string, any>>(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should still unwrap in objects nested in arrays
|
|
|
|
const arr2 = ref([{ a: ref(1) }]).value
|
|
|
|
expectType<number>(arr2[0].a)
|
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)
|
2020-04-16 21:24:46 +08:00
|
|
|
|
|
|
|
function withSymbol() {
|
|
|
|
const customSymbol = Symbol()
|
|
|
|
const obj = {
|
|
|
|
[Symbol.asyncIterator]: { a: 1 },
|
|
|
|
[Symbol.unscopables]: { b: '1' },
|
|
|
|
[customSymbol]: { c: [1, 2, 3] }
|
|
|
|
}
|
|
|
|
|
|
|
|
const objRef = ref(obj)
|
|
|
|
|
|
|
|
expectType<{ a: number }>(objRef.value[Symbol.asyncIterator])
|
|
|
|
expectType<{ b: string }>(objRef.value[Symbol.unscopables])
|
|
|
|
expectType<{ c: Array<number> }>(objRef.value[customSymbol])
|
|
|
|
}
|
|
|
|
|
|
|
|
withSymbol()
|
2020-05-04 20:51:01 +08:00
|
|
|
|
|
|
|
const state = reactive({
|
|
|
|
foo: {
|
|
|
|
value: 1,
|
|
|
|
label: 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expectType<string>(state.foo.label)
|