2
0

defineCustomElement.test-d.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {
  2. type VueElementConstructor,
  3. defineComponent,
  4. defineCustomElement,
  5. } from 'vue'
  6. import { describe, expectType, test } from './utils'
  7. describe('inject', () => {
  8. // with object inject
  9. defineCustomElement({
  10. props: {
  11. a: String,
  12. },
  13. inject: {
  14. foo: 'foo',
  15. bar: 'bar',
  16. },
  17. created() {
  18. expectType<unknown>(this.foo)
  19. expectType<unknown>(this.bar)
  20. // @ts-expect-error
  21. this.foobar = 1
  22. },
  23. })
  24. // with array inject
  25. defineCustomElement({
  26. props: ['a', 'b'],
  27. inject: ['foo', 'bar'],
  28. created() {
  29. expectType<unknown>(this.foo)
  30. expectType<unknown>(this.bar)
  31. // @ts-expect-error
  32. this.foobar = 1
  33. },
  34. })
  35. // with no props
  36. defineCustomElement({
  37. inject: {
  38. foo: {
  39. from: 'pbar',
  40. default: 'foo',
  41. },
  42. bar: {
  43. from: 'pfoo',
  44. default: 'bar',
  45. },
  46. },
  47. created() {
  48. expectType<unknown>(this.foo)
  49. expectType<unknown>(this.bar)
  50. // @ts-expect-error
  51. this.foobar = 1
  52. },
  53. })
  54. // without inject
  55. defineCustomElement({
  56. props: ['a', 'b'],
  57. created() {
  58. // @ts-expect-error
  59. this.foo = 1
  60. // @ts-expect-error
  61. this.bar = 1
  62. },
  63. })
  64. })
  65. describe('defineCustomElement using defineComponent return type', () => {
  66. test('with object emits', () => {
  67. const Comp1Vue = defineComponent({
  68. props: {
  69. a: String,
  70. },
  71. emits: {
  72. click: () => true,
  73. },
  74. })
  75. const Comp = defineCustomElement(Comp1Vue)
  76. expectType<VueElementConstructor>(Comp)
  77. const instance = new Comp()
  78. expectType<string | undefined>(instance.a)
  79. instance.a = ''
  80. })
  81. test('with array emits', () => {
  82. const Comp1Vue = defineComponent({
  83. props: {
  84. a: Number,
  85. },
  86. emits: ['click'],
  87. })
  88. const Comp = defineCustomElement(Comp1Vue)
  89. expectType<VueElementConstructor>(Comp)
  90. const instance = new Comp()
  91. expectType<number | undefined>(instance.a)
  92. instance.a = 42
  93. })
  94. test('with required props', () => {
  95. const Comp1Vue = defineComponent({
  96. props: {
  97. a: { type: Number, required: true },
  98. },
  99. })
  100. const Comp = defineCustomElement(Comp1Vue)
  101. expectType<VueElementConstructor>(Comp)
  102. const instance = new Comp()
  103. expectType<number>(instance.a)
  104. instance.a = 42
  105. })
  106. test('with default props', () => {
  107. const Comp1Vue = defineComponent({
  108. props: {
  109. a: {
  110. type: Number,
  111. default: 1,
  112. validator: () => true,
  113. },
  114. },
  115. emits: ['click'],
  116. })
  117. const Comp = defineCustomElement(Comp1Vue)
  118. expectType<VueElementConstructor>(Comp)
  119. const instance = new Comp()
  120. expectType<number>(instance.a)
  121. instance.a = 42
  122. })
  123. })