defineCustomElement.test-d.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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. expectType<string | undefined>(new Comp().a)
  78. })
  79. })