setup-test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. })