ref.test-d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {
  2. Ref,
  3. ref,
  4. isRef,
  5. unref,
  6. reactive,
  7. expectType,
  8. proxyRefs
  9. } from './index'
  10. function plainType(arg: number | Ref<number>) {
  11. // ref coercing
  12. const coerced = ref(arg)
  13. expectType<Ref<number>>(coerced)
  14. // isRef as type guard
  15. if (isRef(arg)) {
  16. expectType<Ref<number>>(arg)
  17. }
  18. // ref unwrapping
  19. expectType<number>(unref(arg))
  20. // ref inner type should be unwrapped
  21. const nestedRef = ref({
  22. foo: ref(1)
  23. })
  24. expectType<Ref<{ foo: number }>>(nestedRef)
  25. expectType<{ foo: number }>(nestedRef.value)
  26. // ref boolean
  27. const falseRef = ref(false)
  28. expectType<Ref<boolean>>(falseRef)
  29. expectType<boolean>(falseRef.value)
  30. // ref true
  31. const trueRef = ref<true>(true)
  32. expectType<Ref<true>>(trueRef)
  33. expectType<true>(trueRef.value)
  34. // tuple
  35. expectType<[number, string]>(unref(ref([1, '1'])))
  36. interface IteratorFoo {
  37. [Symbol.iterator]: any
  38. }
  39. // with symbol
  40. expectType<Ref<IteratorFoo | null | undefined>>(
  41. ref<IteratorFoo | null | undefined>()
  42. )
  43. // should not unwrap ref inside arrays
  44. const arr = ref([1, new Map<string, any>(), ref('1')]).value
  45. const value = arr[0]
  46. if (isRef(value)) {
  47. expectType<Ref>(value)
  48. } else if (typeof value === 'number') {
  49. expectType<number>(value)
  50. } else {
  51. // should narrow down to Map type
  52. // and not contain any Ref type
  53. expectType<Map<string, any>>(value)
  54. }
  55. // should still unwrap in objects nested in arrays
  56. const arr2 = ref([{ a: ref(1) }]).value
  57. expectType<number>(arr2[0].a)
  58. }
  59. plainType(1)
  60. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  61. // ref coercing
  62. const coerced = ref(arg)
  63. expectType<Ref<HTMLElement>>(coerced)
  64. // isRef as type guard
  65. if (isRef(arg)) {
  66. expectType<Ref<HTMLElement>>(arg)
  67. }
  68. // ref unwrapping
  69. expectType<HTMLElement>(unref(arg))
  70. // ref inner type should be unwrapped
  71. // eslint-disable-next-line no-restricted-globals
  72. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  73. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  74. expectType<{ foo: HTMLElement }>(nestedRef.value)
  75. }
  76. // eslint-disable-next-line no-restricted-globals
  77. const el = document.createElement('DIV')
  78. bailType(el)
  79. function withSymbol() {
  80. const customSymbol = Symbol()
  81. const obj = {
  82. [Symbol.asyncIterator]: { a: 1 },
  83. [Symbol.unscopables]: { b: '1' },
  84. [customSymbol]: { c: [1, 2, 3] }
  85. }
  86. const objRef = ref(obj)
  87. expectType<{ a: number }>(objRef.value[Symbol.asyncIterator])
  88. expectType<{ b: string }>(objRef.value[Symbol.unscopables])
  89. expectType<{ c: Array<number> }>(objRef.value[customSymbol])
  90. }
  91. withSymbol()
  92. const state = reactive({
  93. foo: {
  94. value: 1,
  95. label: 'bar'
  96. }
  97. })
  98. expectType<string>(state.foo.label)
  99. // proxyRefs: should return `reactive` directly
  100. const r1 = reactive({
  101. k: 'v'
  102. })
  103. const p1 = proxyRefs(r1)
  104. expectType<typeof r1>(p1)
  105. // proxyRefs: `ShallowUnwrapRef`
  106. const r2 = {
  107. a: ref(1),
  108. obj: {
  109. k: ref('foo')
  110. }
  111. }
  112. const p2 = proxyRefs(r2)
  113. expectType<number>(p2.a)
  114. expectType<Ref<string>>(p2.obj.k)