ref.test-d.ts 533 B

12345678910111213141516171819202122232425
  1. import { expectType } from 'tsd'
  2. import { Ref, ref, isRef, unref } from './index'
  3. function foo(arg: number | Ref<number>) {
  4. // ref coercing
  5. const coerced = ref(arg)
  6. expectType<Ref<number>>(coerced)
  7. // isRef as type guard
  8. if (isRef(arg)) {
  9. expectType<Ref<number>>(arg)
  10. }
  11. // ref unwrapping
  12. expectType<number>(unref(arg))
  13. // ref inner type should be unwrapped
  14. const nestedRef = ref({
  15. foo: ref(1)
  16. })
  17. expectType<Ref<{ foo: number }>>(nestedRef)
  18. expectType<{ foo: number }>(nestedRef.value)
  19. }
  20. foo(1)