defineComponent.test-d.tsx 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. import {
  2. describe,
  3. Component,
  4. defineComponent,
  5. PropType,
  6. ref,
  7. reactive,
  8. createApp,
  9. expectError,
  10. expectType,
  11. ComponentPublicInstance,
  12. ComponentOptions,
  13. SetupContext,
  14. IsUnion,
  15. h
  16. } from './index'
  17. describe('with object props', () => {
  18. interface ExpectedProps {
  19. a?: number | undefined
  20. b: string
  21. e?: Function
  22. h: boolean
  23. bb: string
  24. bbb: string
  25. cc?: string[] | undefined
  26. dd: { n: 1 }
  27. ee?: () => string
  28. ff?: (a: number, b: string) => { a: boolean }
  29. ccc?: string[] | undefined
  30. ddd: string[]
  31. eee: () => { a: string }
  32. fff: (a: number, b: string) => { a: boolean }
  33. hhh: boolean
  34. ggg: 'foo' | 'bar'
  35. ffff: (a: number, b: string) => { a: boolean }
  36. iii?: (() => string) | (() => number)
  37. jjj: ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
  38. kkk?: any
  39. validated?: string
  40. date?: Date
  41. }
  42. type GT = string & { __brand: unknown }
  43. const MyComponent = defineComponent({
  44. props: {
  45. a: Number,
  46. // required should make property non-void
  47. b: {
  48. type: String,
  49. required: true
  50. },
  51. e: Function,
  52. h: Boolean,
  53. // default value should infer type and make it non-void
  54. bb: {
  55. default: 'hello'
  56. },
  57. bbb: {
  58. // Note: default function value requires arrow syntax + explicit
  59. // annotation
  60. default: (props: any) => (props.bb as string) || 'foo'
  61. },
  62. // explicit type casting
  63. cc: Array as PropType<string[]>,
  64. // required + type casting
  65. dd: {
  66. type: Object as PropType<{ n: 1 }>,
  67. required: true
  68. },
  69. // return type
  70. ee: Function as PropType<() => string>,
  71. // arguments + object return
  72. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  73. // explicit type casting with constructor
  74. ccc: Array as () => string[],
  75. // required + contructor type casting
  76. ddd: {
  77. type: Array as () => string[],
  78. required: true
  79. },
  80. // required + object return
  81. eee: {
  82. type: Function as PropType<() => { a: string }>,
  83. required: true
  84. },
  85. // required + arguments + object return
  86. fff: {
  87. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  88. required: true
  89. },
  90. hhh: {
  91. type: Boolean,
  92. required: true
  93. },
  94. // default + type casting
  95. ggg: {
  96. type: String as PropType<'foo' | 'bar'>,
  97. default: 'foo'
  98. },
  99. // default + function
  100. ffff: {
  101. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  102. default: (a: number, b: string) => ({ a: a > +b })
  103. },
  104. // union + function with different return types
  105. iii: Function as PropType<(() => string) | (() => number)>,
  106. // union + function with different args & same return type
  107. jjj: {
  108. type: Function as PropType<
  109. ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
  110. >,
  111. required: true
  112. },
  113. kkk: null,
  114. validated: {
  115. type: String,
  116. // validator requires explicit annotation
  117. validator: (val: unknown) => val !== ''
  118. },
  119. date: Date
  120. },
  121. setup(props) {
  122. // type assertion. See https://github.com/SamVerschueren/tsd
  123. expectType<ExpectedProps['a']>(props.a)
  124. expectType<ExpectedProps['b']>(props.b)
  125. expectType<ExpectedProps['e']>(props.e)
  126. expectType<ExpectedProps['h']>(props.h)
  127. expectType<ExpectedProps['bb']>(props.bb)
  128. expectType<ExpectedProps['bbb']>(props.bbb)
  129. expectType<ExpectedProps['cc']>(props.cc)
  130. expectType<ExpectedProps['dd']>(props.dd)
  131. expectType<ExpectedProps['ee']>(props.ee)
  132. expectType<ExpectedProps['ff']>(props.ff)
  133. expectType<ExpectedProps['ccc']>(props.ccc)
  134. expectType<ExpectedProps['ddd']>(props.ddd)
  135. expectType<ExpectedProps['eee']>(props.eee)
  136. expectType<ExpectedProps['fff']>(props.fff)
  137. expectType<ExpectedProps['hhh']>(props.hhh)
  138. expectType<ExpectedProps['ggg']>(props.ggg)
  139. expectType<ExpectedProps['ffff']>(props.ffff)
  140. if (typeof props.iii !== 'function') {
  141. expectType<undefined>(props.iii)
  142. }
  143. expectType<ExpectedProps['iii']>(props.iii)
  144. expectType<IsUnion<typeof props.jjj>>(true)
  145. expectType<ExpectedProps['jjj']>(props.jjj)
  146. expectType<ExpectedProps['kkk']>(props.kkk)
  147. expectType<ExpectedProps['validated']>(props.validated)
  148. expectType<ExpectedProps['date']>(props.date)
  149. // @ts-expect-error props should be readonly
  150. expectError((props.a = 1))
  151. // setup context
  152. return {
  153. c: ref(1),
  154. d: {
  155. e: ref('hi')
  156. },
  157. f: reactive({
  158. g: ref('hello' as GT)
  159. })
  160. }
  161. },
  162. render() {
  163. const props = this.$props
  164. expectType<ExpectedProps['a']>(props.a)
  165. expectType<ExpectedProps['b']>(props.b)
  166. expectType<ExpectedProps['e']>(props.e)
  167. expectType<ExpectedProps['h']>(props.h)
  168. expectType<ExpectedProps['bb']>(props.bb)
  169. expectType<ExpectedProps['cc']>(props.cc)
  170. expectType<ExpectedProps['dd']>(props.dd)
  171. expectType<ExpectedProps['ee']>(props.ee)
  172. expectType<ExpectedProps['ff']>(props.ff)
  173. expectType<ExpectedProps['ccc']>(props.ccc)
  174. expectType<ExpectedProps['ddd']>(props.ddd)
  175. expectType<ExpectedProps['eee']>(props.eee)
  176. expectType<ExpectedProps['fff']>(props.fff)
  177. expectType<ExpectedProps['hhh']>(props.hhh)
  178. expectType<ExpectedProps['ggg']>(props.ggg)
  179. if (typeof props.iii !== 'function') {
  180. expectType<undefined>(props.iii)
  181. }
  182. expectType<ExpectedProps['iii']>(props.iii)
  183. expectType<IsUnion<typeof props.jjj>>(true)
  184. expectType<ExpectedProps['jjj']>(props.jjj)
  185. expectType<ExpectedProps['kkk']>(props.kkk)
  186. // @ts-expect-error props should be readonly
  187. expectError((props.a = 1))
  188. // should also expose declared props on `this`
  189. expectType<ExpectedProps['a']>(this.a)
  190. expectType<ExpectedProps['b']>(this.b)
  191. expectType<ExpectedProps['e']>(this.e)
  192. expectType<ExpectedProps['h']>(this.h)
  193. expectType<ExpectedProps['bb']>(this.bb)
  194. expectType<ExpectedProps['cc']>(this.cc)
  195. expectType<ExpectedProps['dd']>(this.dd)
  196. expectType<ExpectedProps['ee']>(this.ee)
  197. expectType<ExpectedProps['ff']>(this.ff)
  198. expectType<ExpectedProps['ccc']>(this.ccc)
  199. expectType<ExpectedProps['ddd']>(this.ddd)
  200. expectType<ExpectedProps['eee']>(this.eee)
  201. expectType<ExpectedProps['fff']>(this.fff)
  202. expectType<ExpectedProps['hhh']>(this.hhh)
  203. expectType<ExpectedProps['ggg']>(this.ggg)
  204. if (typeof this.iii !== 'function') {
  205. expectType<undefined>(this.iii)
  206. }
  207. expectType<ExpectedProps['iii']>(this.iii)
  208. const { jjj } = this
  209. expectType<IsUnion<typeof jjj>>(true)
  210. expectType<ExpectedProps['jjj']>(this.jjj)
  211. expectType<ExpectedProps['kkk']>(this.kkk)
  212. // @ts-expect-error props on `this` should be readonly
  213. expectError((this.a = 1))
  214. // assert setup context unwrapping
  215. expectType<number>(this.c)
  216. expectType<string>(this.d.e.value)
  217. expectType<GT>(this.f.g)
  218. // setup context properties should be mutable
  219. this.c = 2
  220. return null
  221. }
  222. })
  223. expectType<Component>(MyComponent)
  224. // Test TSX
  225. expectType<JSX.Element>(
  226. <MyComponent
  227. a={1}
  228. b="b"
  229. bb="bb"
  230. e={() => {}}
  231. cc={['cc']}
  232. dd={{ n: 1 }}
  233. ee={() => 'ee'}
  234. ccc={['ccc']}
  235. ddd={['ddd']}
  236. eee={() => ({ a: 'eee' })}
  237. fff={(a, b) => ({ a: a > +b })}
  238. hhh={false}
  239. ggg="foo"
  240. jjj={() => ''}
  241. // should allow class/style as attrs
  242. class="bar"
  243. style={{ color: 'red' }}
  244. // should allow key
  245. key={'foo'}
  246. // should allow ref
  247. ref={'foo'}
  248. />
  249. )
  250. expectType<Component>(
  251. <MyComponent
  252. b="b"
  253. dd={{ n: 1 }}
  254. ddd={['ddd']}
  255. eee={() => ({ a: 'eee' })}
  256. fff={(a, b) => ({ a: a > +b })}
  257. hhh={false}
  258. jjj={() => ''}
  259. />
  260. )
  261. // @ts-expect-error missing required props
  262. expectError(<MyComponent />)
  263. expectError(
  264. // @ts-expect-error wrong prop types
  265. <MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
  266. )
  267. expectError(
  268. // @ts-expect-error wrong prop types
  269. <MyComponent ggg="baz" />
  270. )
  271. // @ts-expect-error
  272. expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
  273. // `this` should be void inside of prop validators and prop default factories
  274. defineComponent({
  275. props: {
  276. myProp: {
  277. type: Number,
  278. validator(val: unknown): boolean {
  279. // @ts-expect-error
  280. return val !== this.otherProp
  281. },
  282. default(): number {
  283. // @ts-expect-error
  284. return this.otherProp + 1
  285. }
  286. },
  287. otherProp: {
  288. type: Number,
  289. required: true
  290. }
  291. }
  292. })
  293. })
  294. // describe('type inference w/ optional props declaration', () => {
  295. // const MyComponent = defineComponent({
  296. // setup(_props: { msg: string }) {
  297. // return {
  298. // a: 1
  299. // }
  300. // },
  301. // render() {
  302. // expectType<string>(this.$props.msg)
  303. // // props should be readonly
  304. // expectError((this.$props.msg = 'foo'))
  305. // // should not expose on `this`
  306. // expectError(this.msg)
  307. // expectType<number>(this.a)
  308. // return null
  309. // }
  310. // })
  311. // expectType<JSX.Element>(<MyComponent msg="foo" />)
  312. // expectError(<MyComponent />)
  313. // expectError(<MyComponent msg={1} />)
  314. // })
  315. // describe('type inference w/ direct setup function', () => {
  316. // const MyComponent = defineComponent((_props: { msg: string }) => {})
  317. // expectType<JSX.Element>(<MyComponent msg="foo" />)
  318. // expectError(<MyComponent />)
  319. // expectError(<MyComponent msg={1} />)
  320. // })
  321. describe('type inference w/ array props declaration', () => {
  322. const MyComponent = defineComponent({
  323. props: ['a', 'b'],
  324. setup(props) {
  325. // @ts-expect-error props should be readonly
  326. expectError((props.a = 1))
  327. expectType<any>(props.a)
  328. expectType<any>(props.b)
  329. return {
  330. c: 1
  331. }
  332. },
  333. render() {
  334. expectType<any>(this.$props.a)
  335. expectType<any>(this.$props.b)
  336. // @ts-expect-error
  337. expectError((this.$props.a = 1))
  338. expectType<any>(this.a)
  339. expectType<any>(this.b)
  340. expectType<number>(this.c)
  341. }
  342. })
  343. expectType<JSX.Element>(<MyComponent a={[1, 2]} b="b" />)
  344. // @ts-expect-error
  345. expectError(<MyComponent other="other" />)
  346. })
  347. describe('type inference w/ options API', () => {
  348. defineComponent({
  349. props: { a: Number },
  350. setup() {
  351. return {
  352. b: 123
  353. }
  354. },
  355. data() {
  356. // Limitation: we cannot expose the return result of setup() on `this`
  357. // here in data() - somehow that would mess up the inference
  358. expectType<number | undefined>(this.a)
  359. return {
  360. c: this.a || 123
  361. }
  362. },
  363. computed: {
  364. d(): number {
  365. expectType<number>(this.b)
  366. return this.b + 1
  367. },
  368. e: {
  369. get(): number {
  370. expectType<number>(this.b)
  371. expectType<number>(this.d)
  372. return this.b + this.d
  373. },
  374. set(v: number) {
  375. expectType<number>(this.b)
  376. expectType<number>(this.d)
  377. expectType<number>(v)
  378. }
  379. }
  380. },
  381. watch: {
  382. a() {
  383. expectType<number>(this.b)
  384. this.b + 1
  385. }
  386. },
  387. created() {
  388. // props
  389. expectType<number | undefined>(this.a)
  390. // returned from setup()
  391. expectType<number>(this.b)
  392. // returned from data()
  393. expectType<number>(this.c)
  394. // computed
  395. expectType<number>(this.d)
  396. // computed get/set
  397. expectType<number>(this.e)
  398. },
  399. methods: {
  400. doSomething() {
  401. // props
  402. expectType<number | undefined>(this.a)
  403. // returned from setup()
  404. expectType<number>(this.b)
  405. // returned from data()
  406. expectType<number>(this.c)
  407. // computed
  408. expectType<number>(this.d)
  409. // computed get/set
  410. expectType<number>(this.e)
  411. },
  412. returnSomething() {
  413. return this.a
  414. }
  415. },
  416. render() {
  417. // props
  418. expectType<number | undefined>(this.a)
  419. // returned from setup()
  420. expectType<number>(this.b)
  421. // returned from data()
  422. expectType<number>(this.c)
  423. // computed
  424. expectType<number>(this.d)
  425. // computed get/set
  426. expectType<number>(this.e)
  427. // method
  428. expectType<() => number | undefined>(this.returnSomething)
  429. }
  430. })
  431. })
  432. describe('with mixins', () => {
  433. const MixinA = defineComponent({
  434. props: {
  435. aP1: {
  436. type: String,
  437. default: 'aP1'
  438. },
  439. aP2: Boolean
  440. },
  441. data() {
  442. return {
  443. a: 1
  444. }
  445. }
  446. })
  447. const MixinB = defineComponent({
  448. props: ['bP1', 'bP2'],
  449. data() {
  450. return {
  451. b: 2
  452. }
  453. }
  454. })
  455. const MixinC = defineComponent({
  456. data() {
  457. return {
  458. c: 3
  459. }
  460. }
  461. })
  462. const MixinD = defineComponent({
  463. mixins: [MixinA],
  464. data() {
  465. //@ts-expect-error computed are not available on data()
  466. expectError<number>(this.dC1)
  467. //@ts-expect-error computed are not available on data()
  468. expectError<string>(this.dC2)
  469. return {
  470. d: 4
  471. }
  472. },
  473. setup(props) {
  474. expectType<string>(props.aP1)
  475. },
  476. computed: {
  477. dC1(): number {
  478. return this.d + this.a
  479. },
  480. dC2(): string {
  481. return this.aP1 + 'dC2'
  482. }
  483. }
  484. })
  485. const MyComponent = defineComponent({
  486. mixins: [MixinA, MixinB, MixinC, MixinD],
  487. props: {
  488. // required should make property non-void
  489. z: {
  490. type: String,
  491. required: true
  492. }
  493. },
  494. data(vm) {
  495. expectType<number>(vm.a)
  496. expectType<number>(vm.b)
  497. expectType<number>(vm.c)
  498. expectType<number>(vm.d)
  499. // should also expose declared props on `this`
  500. expectType<number>(this.a)
  501. expectType<string>(this.aP1)
  502. expectType<boolean | undefined>(this.aP2)
  503. expectType<number>(this.b)
  504. expectType<any>(this.bP1)
  505. expectType<number>(this.c)
  506. expectType<number>(this.d)
  507. return {}
  508. },
  509. setup(props) {
  510. expectType<string>(props.z)
  511. // props
  512. expectType<string>(props.aP1)
  513. expectType<boolean | undefined>(props.aP2)
  514. expectType<any>(props.bP1)
  515. expectType<any>(props.bP2)
  516. expectType<string>(props.z)
  517. },
  518. render() {
  519. const props = this.$props
  520. // props
  521. expectType<string>(props.aP1)
  522. expectType<boolean | undefined>(props.aP2)
  523. expectType<any>(props.bP1)
  524. expectType<any>(props.bP2)
  525. expectType<string>(props.z)
  526. const data = this.$data
  527. expectType<number>(data.a)
  528. expectType<number>(data.b)
  529. expectType<number>(data.c)
  530. expectType<number>(data.d)
  531. // should also expose declared props on `this`
  532. expectType<number>(this.a)
  533. expectType<string>(this.aP1)
  534. expectType<boolean | undefined>(this.aP2)
  535. expectType<number>(this.b)
  536. expectType<any>(this.bP1)
  537. expectType<number>(this.c)
  538. expectType<number>(this.d)
  539. expectType<number>(this.dC1)
  540. expectType<string>(this.dC2)
  541. // props should be readonly
  542. // @ts-expect-error
  543. expectError((this.aP1 = 'new'))
  544. // @ts-expect-error
  545. expectError((this.z = 1))
  546. // props on `this` should be readonly
  547. // @ts-expect-error
  548. expectError((this.bP1 = 1))
  549. // string value can not assigned to number type value
  550. // @ts-expect-error
  551. expectError((this.c = '1'))
  552. // setup context properties should be mutable
  553. this.d = 5
  554. return null
  555. }
  556. })
  557. // Test TSX
  558. expectType<JSX.Element>(
  559. <MyComponent aP1={'aP'} aP2 bP1={1} bP2={[1, 2]} z={'z'} />
  560. )
  561. // missing required props
  562. // @ts-expect-error
  563. expectError(<MyComponent />)
  564. // wrong prop types
  565. // @ts-expect-error
  566. expectError(<MyComponent aP1="ap" aP2={'wrong type'} bP1="b" z={'z'} />)
  567. // @ts-expect-error
  568. expectError(<MyComponent aP1={1} bP2={[1]} />)
  569. })
  570. describe('with extends', () => {
  571. const Base = defineComponent({
  572. props: {
  573. aP1: Boolean,
  574. aP2: {
  575. type: Number,
  576. default: 2
  577. }
  578. },
  579. data() {
  580. return {
  581. a: 1
  582. }
  583. },
  584. computed: {
  585. c(): number {
  586. return this.aP2 + this.a
  587. }
  588. }
  589. })
  590. const MyComponent = defineComponent({
  591. extends: Base,
  592. props: {
  593. // required should make property non-void
  594. z: {
  595. type: String,
  596. required: true
  597. }
  598. },
  599. render() {
  600. const props = this.$props
  601. // props
  602. expectType<boolean | undefined>(props.aP1)
  603. expectType<number>(props.aP2)
  604. expectType<string>(props.z)
  605. const data = this.$data
  606. expectType<number>(data.a)
  607. // should also expose declared props on `this`
  608. expectType<number>(this.a)
  609. expectType<boolean | undefined>(this.aP1)
  610. expectType<number>(this.aP2)
  611. // setup context properties should be mutable
  612. this.a = 5
  613. return null
  614. }
  615. })
  616. // Test TSX
  617. expectType<JSX.Element>(<MyComponent aP2={3} aP1 z={'z'} />)
  618. // missing required props
  619. // @ts-expect-error
  620. expectError(<MyComponent />)
  621. // wrong prop types
  622. // @ts-expect-error
  623. expectError(<MyComponent aP2={'wrong type'} z={'z'} />)
  624. // @ts-expect-error
  625. expectError(<MyComponent aP1={3} />)
  626. })
  627. describe('extends with mixins', () => {
  628. const Mixin = defineComponent({
  629. props: {
  630. mP1: {
  631. type: String,
  632. default: 'mP1'
  633. },
  634. mP2: Boolean,
  635. mP3: {
  636. type: Boolean,
  637. required: true
  638. }
  639. },
  640. data() {
  641. return {
  642. a: 1
  643. }
  644. }
  645. })
  646. const Base = defineComponent({
  647. props: {
  648. p1: Boolean,
  649. p2: {
  650. type: Number,
  651. default: 2
  652. },
  653. p3: {
  654. type: Boolean,
  655. required: true
  656. }
  657. },
  658. data() {
  659. return {
  660. b: 2
  661. }
  662. },
  663. computed: {
  664. c(): number {
  665. return this.p2 + this.b
  666. }
  667. }
  668. })
  669. const MyComponent = defineComponent({
  670. extends: Base,
  671. mixins: [Mixin],
  672. props: {
  673. // required should make property non-void
  674. z: {
  675. type: String,
  676. required: true
  677. }
  678. },
  679. render() {
  680. const props = this.$props
  681. // props
  682. expectType<boolean | undefined>(props.p1)
  683. expectType<number>(props.p2)
  684. expectType<string>(props.z)
  685. expectType<string>(props.mP1)
  686. expectType<boolean | undefined>(props.mP2)
  687. const data = this.$data
  688. expectType<number>(data.a)
  689. expectType<number>(data.b)
  690. // should also expose declared props on `this`
  691. expectType<number>(this.a)
  692. expectType<number>(this.b)
  693. expectType<boolean | undefined>(this.p1)
  694. expectType<number>(this.p2)
  695. expectType<string>(this.mP1)
  696. expectType<boolean | undefined>(this.mP2)
  697. // setup context properties should be mutable
  698. this.a = 5
  699. return null
  700. }
  701. })
  702. // Test TSX
  703. expectType<JSX.Element>(<MyComponent mP1="p1" mP2 mP3 p1 p2={1} p3 z={'z'} />)
  704. // mP1, mP2, p1, and p2 have default value. these are not required
  705. expectType<JSX.Element>(<MyComponent mP3 p3 z={'z'} />)
  706. // missing required props
  707. // @ts-expect-error
  708. expectError(<MyComponent mP3 p3 /* z='z' */ />)
  709. // missing required props from mixin
  710. // @ts-expect-error
  711. expectError(<MyComponent /* mP3 */ p3 z="z" />)
  712. // missing required props from extends
  713. // @ts-expect-error
  714. expectError(<MyComponent mP3 /* p3 */ z="z" />)
  715. // wrong prop types
  716. // @ts-expect-error
  717. expectError(<MyComponent p2={'wrong type'} z={'z'} />)
  718. // @ts-expect-error
  719. expectError(<MyComponent mP1={3} />)
  720. })
  721. describe('compatibility w/ createApp', () => {
  722. const comp = defineComponent({})
  723. createApp(comp).mount('#hello')
  724. const comp2 = defineComponent({
  725. props: { foo: String }
  726. })
  727. createApp(comp2).mount('#hello')
  728. const comp3 = defineComponent({
  729. setup() {
  730. return {
  731. a: 1
  732. }
  733. }
  734. })
  735. createApp(comp3).mount('#hello')
  736. })
  737. describe('defineComponent', () => {
  738. test('should accept components defined with defineComponent', () => {
  739. const comp = defineComponent({})
  740. defineComponent({
  741. components: { comp }
  742. })
  743. })
  744. test('should accept class components with receiving constructor arguments', () => {
  745. class Comp {
  746. static __vccOpts = {}
  747. constructor(_props: { foo: string }) {}
  748. }
  749. defineComponent({
  750. components: { Comp }
  751. })
  752. })
  753. })
  754. describe('emits', () => {
  755. // Note: for TSX inference, ideally we want to map emits to onXXX props,
  756. // but that requires type-level string constant concatenation as suggested in
  757. // https://github.com/Microsoft/TypeScript/issues/12754
  758. // The workaround for TSX users is instead of using emits, declare onXXX props
  759. // and call them instead. Since `v-on:click` compiles to an `onClick` prop,
  760. // this would also support other users consuming the component in templates
  761. // with `v-on` listeners.
  762. // with object emits
  763. defineComponent({
  764. emits: {
  765. click: (n: number) => typeof n === 'number',
  766. input: (b: string) => b.length > 1
  767. },
  768. setup(props, { emit }) {
  769. emit('click', 1)
  770. emit('input', 'foo')
  771. // @ts-expect-error
  772. expectError(emit('nope'))
  773. // @ts-expect-error
  774. expectError(emit('click'))
  775. // @ts-expect-error
  776. expectError(emit('click', 'foo'))
  777. // @ts-expect-error
  778. expectError(emit('input'))
  779. // @ts-expect-error
  780. expectError(emit('input', 1))
  781. },
  782. created() {
  783. this.$emit('click', 1)
  784. this.$emit('input', 'foo')
  785. // @ts-expect-error
  786. expectError(this.$emit('nope'))
  787. // @ts-expect-error
  788. expectError(this.$emit('click'))
  789. // @ts-expect-error
  790. expectError(this.$emit('click', 'foo'))
  791. // @ts-expect-error
  792. expectError(this.$emit('input'))
  793. // @ts-expect-error
  794. expectError(this.$emit('input', 1))
  795. }
  796. })
  797. // with array emits
  798. defineComponent({
  799. emits: ['foo', 'bar'],
  800. setup(props, { emit }) {
  801. emit('foo')
  802. emit('foo', 123)
  803. emit('bar')
  804. // @ts-expect-error
  805. expectError(emit('nope'))
  806. },
  807. created() {
  808. this.$emit('foo')
  809. this.$emit('foo', 123)
  810. this.$emit('bar')
  811. // @ts-expect-error
  812. expectError(this.$emit('nope'))
  813. }
  814. })
  815. // without emits
  816. defineComponent({
  817. setup(props, { emit }) {
  818. emit('test', 1)
  819. emit('test')
  820. }
  821. })
  822. // emit should be valid when ComponentPublicInstance is used.
  823. const instance = {} as ComponentPublicInstance
  824. instance.$emit('test', 1)
  825. instance.$emit('test')
  826. // `this` should be void inside of emits validators
  827. defineComponent({
  828. props: ['bar'],
  829. emits: {
  830. foo(): boolean {
  831. // @ts-expect-error
  832. return this.bar === 3
  833. }
  834. }
  835. })
  836. })
  837. describe('componentOptions setup should be `SetupContext`', () => {
  838. expect<ComponentOptions['setup']>({} as (
  839. props: Record<string, any>,
  840. ctx: SetupContext
  841. ) => any)
  842. })
  843. describe('extract instance type', () => {
  844. const Base = defineComponent({
  845. props: {
  846. baseA: {
  847. type: Number,
  848. default: 1
  849. }
  850. }
  851. })
  852. const MixinA = defineComponent({
  853. props: {
  854. mA: {
  855. type: String,
  856. default: ''
  857. }
  858. }
  859. })
  860. const CompA = defineComponent({
  861. extends: Base,
  862. mixins: [MixinA],
  863. props: {
  864. a: {
  865. type: Boolean,
  866. default: false
  867. },
  868. b: {
  869. type: String,
  870. required: true
  871. },
  872. c: Number
  873. }
  874. })
  875. const compA = {} as InstanceType<typeof CompA>
  876. expectType<boolean>(compA.a)
  877. expectType<string>(compA.b)
  878. expectType<number | undefined>(compA.c)
  879. // mixins
  880. expectType<string>(compA.mA)
  881. // extends
  882. expectType<number>(compA.baseA)
  883. // @ts-expect-error
  884. expectError((compA.a = true))
  885. // @ts-expect-error
  886. expectError((compA.b = 'foo'))
  887. // @ts-expect-error
  888. expectError((compA.c = 1))
  889. // @ts-expect-error
  890. expectError((compA.mA = 'foo'))
  891. // @ts-expect-error
  892. expectError((compA.baseA = 1))
  893. })
  894. describe('async setup', () => {
  895. type GT = string & { __brand: unknown }
  896. const Comp = defineComponent({
  897. async setup() {
  898. // setup context
  899. return {
  900. a: ref(1),
  901. b: {
  902. c: ref('hi')
  903. },
  904. d: reactive({
  905. e: ref('hello' as GT)
  906. })
  907. }
  908. },
  909. render() {
  910. // assert setup context unwrapping
  911. expectType<number>(this.a)
  912. expectType<string>(this.b.c.value)
  913. expectType<GT>(this.d.e)
  914. // setup context properties should be mutable
  915. this.a = 2
  916. }
  917. })
  918. const vm = {} as InstanceType<typeof Comp>
  919. // assert setup context unwrapping
  920. expectType<number>(vm.a)
  921. expectType<string>(vm.b.c.value)
  922. expectType<GT>(vm.d.e)
  923. // setup context properties should be mutable
  924. vm.a = 2
  925. })
  926. // check if defineComponent can be exported
  927. export default {
  928. // function components
  929. a: defineComponent(_ => h('div')),
  930. // no props
  931. b: defineComponent({
  932. data() {
  933. return {}
  934. }
  935. }),
  936. c: defineComponent({
  937. props: ['a']
  938. }),
  939. d: defineComponent({
  940. props: {
  941. a: Number
  942. }
  943. })
  944. }