defineComponent.test-d.tsx 28 KB

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