componentInstance.test-d.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {
  2. type ComponentInstance,
  3. type ComponentPublicInstance,
  4. type FunctionalComponent,
  5. defineComponent,
  6. ref,
  7. } from 'vue'
  8. import { describe, expectType } from './utils'
  9. describe('defineComponent', () => {
  10. const CompSetup = defineComponent({
  11. props: {
  12. test: String,
  13. },
  14. setup() {
  15. return {
  16. a: 1,
  17. }
  18. },
  19. })
  20. const compSetup: ComponentInstance<typeof CompSetup> = {} as any
  21. expectType<string | undefined>(compSetup.test)
  22. expectType<number>(compSetup.a)
  23. expectType<ComponentPublicInstance>(compSetup)
  24. })
  25. describe('functional component', () => {
  26. // Functional
  27. const CompFunctional: FunctionalComponent<{ test?: string }> = {} as any
  28. const compFunctional: ComponentInstance<typeof CompFunctional> = {} as any
  29. expectType<string | undefined>(compFunctional.test)
  30. expectType<ComponentPublicInstance>(compFunctional)
  31. const CompFunction: (props: { test?: string }) => any = {} as any
  32. const compFunction: ComponentInstance<typeof CompFunction> = {} as any
  33. expectType<string | undefined>(compFunction.test)
  34. expectType<ComponentPublicInstance>(compFunction)
  35. })
  36. describe('options component', () => {
  37. // Options
  38. const CompOptions = defineComponent({
  39. props: {
  40. test: String,
  41. },
  42. data() {
  43. return {
  44. a: 1,
  45. }
  46. },
  47. computed: {
  48. b() {
  49. return 'test'
  50. },
  51. },
  52. methods: {
  53. func(a: string) {
  54. return true
  55. },
  56. },
  57. })
  58. const compOptions: ComponentInstance<typeof CompOptions> = {} as any
  59. expectType<string | undefined>(compOptions.test)
  60. expectType<number>(compOptions.a)
  61. expectType<(a: string) => boolean>(compOptions.func)
  62. expectType<ComponentPublicInstance>(compOptions)
  63. })
  64. describe('object no defineComponent', () => {
  65. // object - no defineComponent
  66. const CompObjectSetup = {
  67. props: {
  68. test: String,
  69. },
  70. setup() {
  71. return {
  72. a: 1,
  73. }
  74. },
  75. }
  76. const compObjectSetup: ComponentInstance<typeof CompObjectSetup> = {} as any
  77. expectType<string | undefined>(compObjectSetup.test)
  78. expectType<number>(compObjectSetup.a)
  79. expectType<ComponentPublicInstance>(compObjectSetup)
  80. const CompObjectData = {
  81. props: {
  82. test: String,
  83. },
  84. data() {
  85. return {
  86. a: 1,
  87. }
  88. },
  89. }
  90. const compObjectData: ComponentInstance<typeof CompObjectData> = {} as any
  91. expectType<string | undefined>(compObjectData.test)
  92. expectType<number>(compObjectData.a)
  93. expectType<ComponentPublicInstance>(compObjectData)
  94. const CompObjectNoProps = {
  95. data() {
  96. return {
  97. a: 1,
  98. }
  99. },
  100. }
  101. const compObjectNoProps: ComponentInstance<typeof CompObjectNoProps> =
  102. {} as any
  103. expectType<string | undefined>(compObjectNoProps.test)
  104. expectType<number>(compObjectNoProps.a)
  105. expectType<ComponentPublicInstance>(compObjectNoProps)
  106. })
  107. describe('Generic component', () => {
  108. const Comp = defineComponent(
  109. // TODO: babel plugin to auto infer runtime props options from type
  110. // similar to defineProps<{...}>()
  111. <T extends string | number>(props: { msg: T; list: T[] }) => {
  112. // use Composition API here like in <script setup>
  113. const count = ref(0)
  114. return () => (
  115. // return a render function (both JSX and h() works)
  116. <div>
  117. {props.msg} {count.value}
  118. </div>
  119. )
  120. },
  121. )
  122. // defaults to known types since types are resolved on instantiation
  123. const comp: ComponentInstance<typeof Comp> = {} as any
  124. expectType<string | number>(comp.msg)
  125. expectType<Array<string | number>>(comp.list)
  126. })
  127. // #12751
  128. {
  129. const Comp = defineComponent({
  130. __typeEmits: {} as {
  131. 'update:visible': [value?: boolean]
  132. },
  133. })
  134. const comp: ComponentInstance<typeof Comp> = {} as any
  135. expectType<((value?: boolean) => any) | undefined>(comp['onUpdate:visible'])
  136. expectType<{ 'onUpdate:visible'?: (value?: boolean) => any }>(comp['$props'])
  137. // @ts-expect-error
  138. comp['$props']['$props']
  139. }