defineComponent.test-d.tsx 30 KB

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