setup-test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }
  29. })
  30. // object props
  31. defineComponent({
  32. props: {
  33. foo: String,
  34. bar: Number
  35. },
  36. setup(props) {
  37. // @ts-expect-error
  38. props.foo.slice(1, 2)
  39. props.foo?.slice(1, 2)
  40. // @ts-expect-error
  41. props.bar + 123
  42. props.bar?.toFixed(2)
  43. }
  44. })
  45. // array props
  46. defineComponent({
  47. props: ['foo', 'bar'],
  48. setup(props) {
  49. props.foo
  50. props.bar
  51. }
  52. })
  53. // context
  54. defineComponent({
  55. emits: ['foo'],
  56. setup(_props, ctx) {
  57. if (ctx.attrs.id) {
  58. }
  59. ctx.emit('foo')
  60. // @ts-expect-error
  61. ctx.emit('ok')
  62. ctx.slots.default && ctx.slots.default()
  63. },
  64. methods: {
  65. foo() {
  66. this.$emit('foo')
  67. // @ts-expect-error
  68. this.$emit('bar')
  69. }
  70. }
  71. })
  72. defineComponent({
  73. props: {
  74. foo: null as any as PropType<{ a: number }>
  75. },
  76. data() {
  77. this.foo?.a
  78. },
  79. setup(props) {
  80. const res = props.foo?.a.toFixed(2)
  81. // @ts-expect-error
  82. res.charAt(1)
  83. res?.charAt(1)
  84. }
  85. })
  86. // #12568
  87. const vm = new Vue({
  88. setup() {},
  89. render: h => h({})
  90. })
  91. vm.$mount('#app')