| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import { Ref, ref, isRef, unref, reactive, expectType } 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)
- // 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)
- // tuple
- expectType<[number, string]>(unref(ref([1, '1'])))
- interface IteratorFoo {
- [Symbol.iterator]: any
- }
- // with symbol
- expectType<Ref<IteratorFoo | null | undefined>>(
- ref<IteratorFoo | null | undefined>()
- )
- }
- 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)
- 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()
- const state = reactive({
- foo: {
- value: 1,
- label: 'bar'
- }
- })
- expectType<string>(state.foo.label)
|