ref.test-d.ts 486 B

123456789101112131415161718192021222324
  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. }
  19. foo(1)