defineComponent.test-d.tsx 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. declare module 'vue' {
  18. interface ComponentCustomProps {
  19. hello?: string
  20. }
  21. }
  22. describe('with object props', () => {
  23. interface ExpectedProps {
  24. a?: number | undefined
  25. b: string
  26. e?: Function
  27. h: boolean
  28. j: undefined | (() => string | undefined)
  29. bb: string
  30. bbb: string
  31. bbbb: string | undefined
  32. bbbbb: string | undefined
  33. cc?: string[] | undefined
  34. dd: { n: 1 }
  35. ee?: () => string
  36. ff?: (a: number, b: string) => { a: boolean }
  37. ccc?: string[] | undefined
  38. ddd: string[]
  39. eee: () => { a: string }
  40. fff: (a: number, b: string) => { a: boolean }
  41. hhh: boolean
  42. ggg: 'foo' | 'bar'
  43. ffff: (a: number, b: string) => { a: boolean }
  44. iii?: (() => string) | (() => number)
  45. jjj: ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
  46. kkk?: any
  47. validated?: string
  48. date?: Date
  49. l?: Date
  50. ll?: Date | number
  51. lll?: string | number
  52. }
  53. type GT = string & { __brand: unknown }
  54. const MyComponent = defineComponent({
  55. props: {
  56. a: Number,
  57. // required should make property non-void
  58. b: {
  59. type: String,
  60. required: true
  61. },
  62. e: Function,
  63. h: Boolean,
  64. j: Function as PropType<undefined | (() => string | undefined)>,
  65. // default value should infer type and make it non-void
  66. bb: {
  67. default: 'hello'
  68. },
  69. bbb: {
  70. // Note: default function value requires arrow syntax + explicit
  71. // annotation
  72. default: (props: any) => (props.bb as string) || 'foo'
  73. },
  74. bbbb: {
  75. type: String,
  76. default: undefined
  77. },
  78. bbbbb: {
  79. type: String,
  80. default: () => undefined
  81. },
  82. // explicit type casting
  83. cc: Array as PropType<string[]>,
  84. // required + type casting
  85. dd: {
  86. type: Object as PropType<{ n: 1 }>,
  87. required: true
  88. },
  89. // return type
  90. ee: Function as PropType<() => string>,
  91. // arguments + object return
  92. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  93. // explicit type casting with constructor
  94. ccc: Array as () => string[],
  95. // required + constructor type casting
  96. ddd: {
  97. type: Array as () => string[],
  98. required: true
  99. },
  100. // required + object return
  101. eee: {
  102. type: Function as PropType<() => { a: string }>,
  103. required: true
  104. },
  105. // required + arguments + object return
  106. fff: {
  107. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  108. required: true
  109. },
  110. hhh: {
  111. type: Boolean,
  112. required: true
  113. },
  114. // default + type casting
  115. ggg: {
  116. type: String as PropType<'foo' | 'bar'>,
  117. default: 'foo'
  118. },
  119. // default + function
  120. ffff: {
  121. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  122. default: (a: number, b: string) => ({ a: a > +b })
  123. },
  124. // union + function with different return types
  125. iii: Function as PropType<(() => string) | (() => number)>,
  126. // union + function with different args & same return type
  127. jjj: {
  128. type: Function as PropType<
  129. ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
  130. >,
  131. required: true
  132. },
  133. kkk: null,
  134. validated: {
  135. type: String,
  136. // validator requires explicit annotation
  137. validator: (val: unknown) => val !== ''
  138. },
  139. date: Date,
  140. l: [Date],
  141. ll: [Date, Number],
  142. lll: [String, Number]
  143. },
  144. setup(props) {
  145. // type assertion. See https://github.com/SamVerschueren/tsd
  146. expectType<ExpectedProps['a']>(props.a)
  147. expectType<ExpectedProps['b']>(props.b)
  148. expectType<ExpectedProps['e']>(props.e)
  149. expectType<ExpectedProps['h']>(props.h)
  150. expectType<ExpectedProps['j']>(props.j)
  151. expectType<ExpectedProps['bb']>(props.bb)
  152. expectType<ExpectedProps['bbb']>(props.bbb)
  153. expectType<ExpectedProps['bbbb']>(props.bbbb)
  154. expectType<ExpectedProps['bbbbb']>(props.bbbbb)
  155. expectType<ExpectedProps['cc']>(props.cc)
  156. expectType<ExpectedProps['dd']>(props.dd)
  157. expectType<ExpectedProps['ee']>(props.ee)
  158. expectType<ExpectedProps['ff']>(props.ff)
  159. expectType<ExpectedProps['ccc']>(props.ccc)
  160. expectType<ExpectedProps['ddd']>(props.ddd)
  161. expectType<ExpectedProps['eee']>(props.eee)
  162. expectType<ExpectedProps['fff']>(props.fff)
  163. expectType<ExpectedProps['hhh']>(props.hhh)
  164. expectType<ExpectedProps['ggg']>(props.ggg)
  165. expectType<ExpectedProps['ffff']>(props.ffff)
  166. if (typeof props.iii !== 'function') {
  167. expectType<undefined>(props.iii)
  168. }
  169. expectType<ExpectedProps['iii']>(props.iii)
  170. expectType<IsUnion<typeof props.jjj>>(true)
  171. expectType<ExpectedProps['jjj']>(props.jjj)
  172. expectType<ExpectedProps['kkk']>(props.kkk)
  173. expectType<ExpectedProps['validated']>(props.validated)
  174. expectType<ExpectedProps['date']>(props.date)
  175. expectType<ExpectedProps['l']>(props.l)
  176. expectType<ExpectedProps['ll']>(props.ll)
  177. expectType<ExpectedProps['lll']>(props.lll)
  178. // @ts-expect-error props should be readonly
  179. expectError((props.a = 1))
  180. // setup context
  181. return {
  182. c: ref(1),
  183. d: {
  184. e: ref('hi')
  185. },
  186. f: reactive({
  187. g: ref('hello' as GT)
  188. })
  189. }
  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. />
  278. )
  279. expectType<Component>(
  280. <MyComponent
  281. b="b"
  282. dd={{ n: 1 }}
  283. ddd={['ddd']}
  284. eee={() => ({ a: 'eee' })}
  285. fff={(a, b) => ({ a: a > +b })}
  286. hhh={false}
  287. jjj={() => ''}
  288. hello="hello"
  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. expect<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. // check if defineComponent can be exported
  1050. export default {
  1051. // function components
  1052. a: defineComponent(_ => h('div')),
  1053. // no props
  1054. b: defineComponent({
  1055. data() {
  1056. return {}
  1057. }
  1058. }),
  1059. c: defineComponent({
  1060. props: ['a']
  1061. }),
  1062. d: defineComponent({
  1063. props: {
  1064. a: Number
  1065. }
  1066. })
  1067. }