setup-test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import Vue, { defineComponent, PropType } from '../../index'
  2. // object props
  3. Vue.extend({
  4. props: {
  5. foo: String,
  6. bar: Number
  7. },
  8. setup(props) {
  9. props.foo + 'foo'
  10. props.bar + 123
  11. }
  12. })
  13. // array props
  14. Vue.extend({
  15. props: ['foo', 'bar'],
  16. setup(props) {
  17. props.foo
  18. props.bar
  19. }
  20. })
  21. // context
  22. Vue.extend({
  23. setup(_props, ctx) {
  24. if (ctx.attrs.id) {
  25. }
  26. ctx.emit('foo')
  27. ctx.slots.default && ctx.slots.default()
  28. ctx.expose({
  29. a: 123
  30. })
  31. }
  32. })
  33. // object props
  34. defineComponent({
  35. props: {
  36. foo: String,
  37. bar: Number
  38. },
  39. setup(props) {
  40. // @ts-expect-error
  41. props.foo.slice(1, 2)
  42. props.foo?.slice(1, 2)
  43. // @ts-expect-error
  44. props.bar + 123
  45. props.bar?.toFixed(2)
  46. }
  47. })
  48. // array props
  49. defineComponent({
  50. props: ['foo', 'bar'],
  51. setup(props) {
  52. props.foo
  53. props.bar
  54. }
  55. })
  56. // context
  57. defineComponent({
  58. emits: ['foo'],
  59. setup(_props, ctx) {
  60. if (ctx.attrs.id) {
  61. }
  62. ctx.emit('foo')
  63. // @ts-expect-error
  64. ctx.emit('ok')
  65. ctx.slots.default && ctx.slots.default()
  66. },
  67. methods: {
  68. foo() {
  69. this.$emit('foo')
  70. // @ts-expect-error
  71. this.$emit('bar')
  72. }
  73. }
  74. })
  75. defineComponent({
  76. props: {
  77. foo: null as any as PropType<{ a: number }>
  78. },
  79. data() {
  80. this.foo?.a
  81. },
  82. setup(props) {
  83. const res = props.foo?.a.toFixed(2)
  84. // @ts-expect-error
  85. res.charAt(1)
  86. res?.charAt(1)
  87. }
  88. })
  89. // #12568
  90. const vm = new Vue({
  91. setup() {},
  92. render: h => h({})
  93. })
  94. vm.$mount('#app')