defineComponent.test-d.tsx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. import {
  2. Component,
  3. defineComponent,
  4. PropType,
  5. ref,
  6. reactive,
  7. createApp,
  8. ComponentPublicInstance,
  9. ComponentOptions,
  10. SetupContext,
  11. h,
  12. SlotsType,
  13. Slots,
  14. VNode
  15. } from 'vue'
  16. import { describe, expectType, IsUnion } from './utils'
  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 props = {
  50. a: Number,
  51. // required should make property non-void
  52. b: {
  53. type: String,
  54. required: true as true
  55. },
  56. e: Function,
  57. h: Boolean,
  58. j: Function as PropType<undefined | (() => string | undefined)>,
  59. // default value should infer type and make it non-void
  60. bb: {
  61. default: 'hello'
  62. },
  63. bbb: {
  64. // Note: default function value requires arrow syntax + explicit
  65. // annotation
  66. default: (props: any) => (props.bb as string) || 'foo'
  67. },
  68. bbbb: {
  69. type: String,
  70. default: undefined
  71. },
  72. bbbbb: {
  73. type: String,
  74. default: () => undefined
  75. },
  76. // explicit type casting
  77. cc: Array as PropType<string[]>,
  78. // required + type casting
  79. dd: {
  80. type: Object as PropType<{ n: 1 }>,
  81. required: true as true
  82. },
  83. // return type
  84. ee: Function as PropType<() => string>,
  85. // arguments + object return
  86. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  87. // explicit type casting with constructor
  88. ccc: Array as () => string[],
  89. // required + constructor type casting
  90. ddd: {
  91. type: Array as () => string[],
  92. required: true as true
  93. },
  94. // required + object return
  95. eee: {
  96. type: Function as PropType<() => { a: string }>,
  97. required: true as true
  98. },
  99. // required + arguments + object return
  100. fff: {
  101. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  102. required: true as true
  103. },
  104. hhh: {
  105. type: Boolean,
  106. required: true as true
  107. },
  108. // default + type casting
  109. ggg: {
  110. type: String as PropType<'foo' | 'bar'>,
  111. default: 'foo'
  112. },
  113. // default + function
  114. ffff: {
  115. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  116. default: (a: number, b: string) => ({ a: a > +b })
  117. },
  118. // union + function with different return types
  119. iii: Function as PropType<(() => string) | (() => number)>,
  120. // union + function with different args & same return type
  121. jjj: {
  122. type: Function as PropType<
  123. ((arg1: string) => string) | ((arg1: string, arg2: string) => string)
  124. >,
  125. required: true as true
  126. },
  127. kkk: null,
  128. validated: {
  129. type: String,
  130. // validator requires explicit annotation
  131. validator: (val: unknown) => val !== ''
  132. },
  133. date: Date,
  134. l: [Date],
  135. ll: [Date, Number],
  136. lll: [String, Number]
  137. }
  138. const MyComponent = defineComponent({
  139. props,
  140. setup(props) {
  141. // type assertion. See https://github.com/SamVerschueren/tsd
  142. expectType<ExpectedProps['a']>(props.a)
  143. expectType<ExpectedProps['b']>(props.b)
  144. expectType<ExpectedProps['e']>(props.e)
  145. expectType<ExpectedProps['h']>(props.h)
  146. expectType<ExpectedProps['j']>(props.j)
  147. expectType<ExpectedProps['bb']>(props.bb)
  148. expectType<ExpectedProps['bbb']>(props.bbb)
  149. expectType<ExpectedProps['bbbb']>(props.bbbb)
  150. expectType<ExpectedProps['bbbbb']>(props.bbbbb)
  151. expectType<ExpectedProps['cc']>(props.cc)
  152. expectType<ExpectedProps['dd']>(props.dd)
  153. expectType<ExpectedProps['ee']>(props.ee)
  154. expectType<ExpectedProps['ff']>(props.ff)
  155. expectType<ExpectedProps['ccc']>(props.ccc)
  156. expectType<ExpectedProps['ddd']>(props.ddd)
  157. expectType<ExpectedProps['eee']>(props.eee)
  158. expectType<ExpectedProps['fff']>(props.fff)
  159. expectType<ExpectedProps['hhh']>(props.hhh)
  160. expectType<ExpectedProps['ggg']>(props.ggg)
  161. expectType<ExpectedProps['ffff']>(props.ffff)
  162. if (typeof props.iii !== 'function') {
  163. expectType<undefined>(props.iii)
  164. }
  165. expectType<ExpectedProps['iii']>(props.iii)
  166. expectType<IsUnion<typeof props.jjj>>(true)
  167. expectType<ExpectedProps['jjj']>(props.jjj)
  168. expectType<ExpectedProps['kkk']>(props.kkk)
  169. expectType<ExpectedProps['validated']>(props.validated)
  170. expectType<ExpectedProps['date']>(props.date)
  171. expectType<ExpectedProps['l']>(props.l)
  172. expectType<ExpectedProps['ll']>(props.ll)
  173. expectType<ExpectedProps['lll']>(props.lll)
  174. // @ts-expect-error props should be readonly
  175. props.a = 1
  176. // setup context
  177. return {
  178. c: ref(1),
  179. d: {
  180. e: ref('hi')
  181. },
  182. f: reactive({
  183. g: ref('hello' as GT)
  184. })
  185. }
  186. },
  187. provide() {
  188. return {}
  189. },
  190. render() {
  191. const props = this.$props
  192. expectType<ExpectedProps['a']>(props.a)
  193. expectType<ExpectedProps['b']>(props.b)
  194. expectType<ExpectedProps['e']>(props.e)
  195. expectType<ExpectedProps['h']>(props.h)
  196. expectType<ExpectedProps['bb']>(props.bb)
  197. expectType<ExpectedProps['cc']>(props.cc)
  198. expectType<ExpectedProps['dd']>(props.dd)
  199. expectType<ExpectedProps['ee']>(props.ee)
  200. expectType<ExpectedProps['ff']>(props.ff)
  201. expectType<ExpectedProps['ccc']>(props.ccc)
  202. expectType<ExpectedProps['ddd']>(props.ddd)
  203. expectType<ExpectedProps['eee']>(props.eee)
  204. expectType<ExpectedProps['fff']>(props.fff)
  205. expectType<ExpectedProps['hhh']>(props.hhh)
  206. expectType<ExpectedProps['ggg']>(props.ggg)
  207. if (typeof props.iii !== 'function') {
  208. expectType<undefined>(props.iii)
  209. }
  210. expectType<ExpectedProps['iii']>(props.iii)
  211. expectType<IsUnion<typeof props.jjj>>(true)
  212. expectType<ExpectedProps['jjj']>(props.jjj)
  213. expectType<ExpectedProps['kkk']>(props.kkk)
  214. // @ts-expect-error props should be readonly
  215. props.a = 1
  216. // should also expose declared props on `this`
  217. expectType<ExpectedProps['a']>(this.a)
  218. expectType<ExpectedProps['b']>(this.b)
  219. expectType<ExpectedProps['e']>(this.e)
  220. expectType<ExpectedProps['h']>(this.h)
  221. expectType<ExpectedProps['bb']>(this.bb)
  222. expectType<ExpectedProps['cc']>(this.cc)
  223. expectType<ExpectedProps['dd']>(this.dd)
  224. expectType<ExpectedProps['ee']>(this.ee)
  225. expectType<ExpectedProps['ff']>(this.ff)
  226. expectType<ExpectedProps['ccc']>(this.ccc)
  227. expectType<ExpectedProps['ddd']>(this.ddd)
  228. expectType<ExpectedProps['eee']>(this.eee)
  229. expectType<ExpectedProps['fff']>(this.fff)
  230. expectType<ExpectedProps['hhh']>(this.hhh)
  231. expectType<ExpectedProps['ggg']>(this.ggg)
  232. if (typeof this.iii !== 'function') {
  233. expectType<undefined>(this.iii)
  234. }
  235. expectType<ExpectedProps['iii']>(this.iii)
  236. const { jjj } = this
  237. expectType<IsUnion<typeof jjj>>(true)
  238. expectType<ExpectedProps['jjj']>(this.jjj)
  239. expectType<ExpectedProps['kkk']>(this.kkk)
  240. // @ts-expect-error props on `this` should be readonly
  241. this.a = 1
  242. // assert setup context unwrapping
  243. expectType<number>(this.c)
  244. expectType<string>(this.d.e.value)
  245. expectType<GT>(this.f.g)
  246. // setup context properties should be mutable
  247. this.c = 2
  248. return null
  249. }
  250. })
  251. expectType<Component>(MyComponent)
  252. // Test TSX
  253. expectType<JSX.Element>(
  254. <MyComponent
  255. a={1}
  256. b="b"
  257. bb="bb"
  258. e={() => {}}
  259. cc={['cc']}
  260. dd={{ n: 1 }}
  261. ee={() => 'ee'}
  262. ccc={['ccc']}
  263. ddd={['ddd']}
  264. eee={() => ({ a: 'eee' })}
  265. fff={(a, b) => ({ a: a > +b })}
  266. hhh={false}
  267. ggg="foo"
  268. jjj={() => ''}
  269. // should allow class/style as attrs
  270. class="bar"
  271. style={{ color: 'red' }}
  272. // should allow key
  273. key={'foo'}
  274. // should allow ref
  275. ref={'foo'}
  276. ref_for={true}
  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. />
  289. )
  290. // @ts-expect-error missing required props
  291. let c = <MyComponent />
  292. // @ts-expect-error wrong prop types
  293. c = <MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
  294. // @ts-expect-error wrong prop types
  295. c = <MyComponent ggg="baz" />
  296. // @ts-expect-error
  297. ;<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />
  298. // `this` should be void inside of prop validators and prop default factories
  299. defineComponent({
  300. props: {
  301. myProp: {
  302. type: Number,
  303. validator(val: unknown): boolean {
  304. // @ts-expect-error
  305. return val !== this.otherProp
  306. },
  307. default(): number {
  308. // @ts-expect-error
  309. return this.otherProp + 1
  310. }
  311. },
  312. otherProp: {
  313. type: Number,
  314. required: true
  315. }
  316. }
  317. })
  318. })
  319. describe('type inference w/ optional props declaration', () => {
  320. const MyComponent = defineComponent<{ a: string[]; msg: string }>({
  321. setup(props) {
  322. expectType<string>(props.msg)
  323. expectType<string[]>(props.a)
  324. return {
  325. b: 1
  326. }
  327. }
  328. })
  329. expectType<JSX.Element>(<MyComponent msg="1" a={['1']} />)
  330. // @ts-expect-error
  331. ;<MyComponent />
  332. // @ts-expect-error
  333. ;<MyComponent msg="1" />
  334. })
  335. describe('type inference w/ direct setup function', () => {
  336. const MyComponent = defineComponent((_props: { msg: string }) => () => {})
  337. expectType<JSX.Element>(<MyComponent msg="foo" />)
  338. // @ts-expect-error
  339. ;<MyComponent />
  340. // @ts-expect-error
  341. ;<MyComponent msg={1} />
  342. })
  343. describe('type inference w/ array props declaration', () => {
  344. const MyComponent = defineComponent({
  345. props: ['a', 'b'],
  346. setup(props) {
  347. // @ts-expect-error props should be readonly
  348. props.a = 1
  349. expectType<any>(props.a)
  350. expectType<any>(props.b)
  351. return {
  352. c: 1
  353. }
  354. },
  355. render() {
  356. expectType<any>(this.$props.a)
  357. expectType<any>(this.$props.b)
  358. // @ts-expect-error
  359. this.$props.a = 1
  360. expectType<any>(this.a)
  361. expectType<any>(this.b)
  362. expectType<number>(this.c)
  363. }
  364. })
  365. expectType<JSX.Element>(<MyComponent a={[1, 2]} b="b" />)
  366. // @ts-expect-error
  367. ;<MyComponent other="other" />
  368. })
  369. describe('type inference w/ options API', () => {
  370. defineComponent({
  371. props: { a: Number },
  372. setup() {
  373. return {
  374. b: 123
  375. }
  376. },
  377. data() {
  378. // Limitation: we cannot expose the return result of setup() on `this`
  379. // here in data() - somehow that would mess up the inference
  380. expectType<number | undefined>(this.a)
  381. return {
  382. c: this.a || 123,
  383. someRef: ref(0)
  384. }
  385. },
  386. computed: {
  387. d() {
  388. expectType<number>(this.b)
  389. return this.b + 1
  390. },
  391. e: {
  392. get() {
  393. expectType<number>(this.b)
  394. expectType<number>(this.d)
  395. return this.b + this.d
  396. },
  397. set(v: number) {
  398. expectType<number>(this.b)
  399. expectType<number>(this.d)
  400. expectType<number>(v)
  401. }
  402. }
  403. },
  404. watch: {
  405. a() {
  406. expectType<number>(this.b)
  407. this.b + 1
  408. }
  409. },
  410. created() {
  411. // props
  412. expectType<number | undefined>(this.a)
  413. // returned from setup()
  414. expectType<number>(this.b)
  415. // returned from data()
  416. expectType<number>(this.c)
  417. // computed
  418. expectType<number>(this.d)
  419. // computed get/set
  420. expectType<number>(this.e)
  421. expectType<number>(this.someRef)
  422. },
  423. methods: {
  424. doSomething() {
  425. // props
  426. expectType<number | undefined>(this.a)
  427. // returned from setup()
  428. expectType<number>(this.b)
  429. // returned from data()
  430. expectType<number>(this.c)
  431. // computed
  432. expectType<number>(this.d)
  433. // computed get/set
  434. expectType<number>(this.e)
  435. },
  436. returnSomething() {
  437. return this.a
  438. }
  439. },
  440. render() {
  441. // props
  442. expectType<number | undefined>(this.a)
  443. // returned from setup()
  444. expectType<number>(this.b)
  445. // returned from data()
  446. expectType<number>(this.c)
  447. // computed
  448. expectType<number>(this.d)
  449. // computed get/set
  450. expectType<number>(this.e)
  451. // method
  452. expectType<() => number | undefined>(this.returnSomething)
  453. }
  454. })
  455. })
  456. describe('with mixins', () => {
  457. const MixinA = defineComponent({
  458. emits: ['bar'],
  459. props: {
  460. aP1: {
  461. type: String,
  462. default: 'aP1'
  463. },
  464. aP2: Boolean
  465. },
  466. data() {
  467. return {
  468. a: 1
  469. }
  470. }
  471. })
  472. const MixinB = defineComponent({
  473. props: ['bP1', 'bP2'],
  474. data() {
  475. return {
  476. b: 2
  477. }
  478. }
  479. })
  480. const MixinC = defineComponent({
  481. data() {
  482. return {
  483. c: 3
  484. }
  485. }
  486. })
  487. const MixinD = defineComponent({
  488. mixins: [MixinA],
  489. data() {
  490. //@ts-expect-error computed are not available on data()
  491. expectError<number>(this.dC1)
  492. //@ts-expect-error computed are not available on data()
  493. expectError<string>(this.dC2)
  494. return {
  495. d: 4
  496. }
  497. },
  498. setup(props) {
  499. expectType<string>(props.aP1)
  500. },
  501. computed: {
  502. dC1() {
  503. return this.d + this.a
  504. },
  505. dC2() {
  506. return this.aP1 + 'dC2'
  507. }
  508. }
  509. })
  510. const MyComponent = defineComponent({
  511. mixins: [MixinA, MixinB, MixinC, MixinD],
  512. emits: ['click'],
  513. props: {
  514. // required should make property non-void
  515. z: {
  516. type: String,
  517. required: true
  518. }
  519. },
  520. data(vm) {
  521. expectType<number>(vm.a)
  522. expectType<number>(vm.b)
  523. expectType<number>(vm.c)
  524. expectType<number>(vm.d)
  525. // should also expose declared props on `this`
  526. expectType<number>(this.a)
  527. expectType<string>(this.aP1)
  528. expectType<boolean | undefined>(this.aP2)
  529. expectType<number>(this.b)
  530. expectType<any>(this.bP1)
  531. expectType<number>(this.c)
  532. expectType<number>(this.d)
  533. return {}
  534. },
  535. setup(props) {
  536. expectType<string>(props.z)
  537. // props
  538. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  539. // from MixinA
  540. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  541. expectType<string>(props.aP1)
  542. expectType<boolean | undefined>(props.aP2)
  543. expectType<any>(props.bP1)
  544. expectType<any>(props.bP2)
  545. expectType<string>(props.z)
  546. },
  547. render() {
  548. const props = this.$props
  549. // props
  550. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  551. // from MixinA
  552. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  553. expectType<string>(props.aP1)
  554. expectType<boolean | undefined>(props.aP2)
  555. expectType<any>(props.bP1)
  556. expectType<any>(props.bP2)
  557. expectType<string>(props.z)
  558. const data = this.$data
  559. expectType<number>(data.a)
  560. expectType<number>(data.b)
  561. expectType<number>(data.c)
  562. expectType<number>(data.d)
  563. // should also expose declared props on `this`
  564. expectType<number>(this.a)
  565. expectType<string>(this.aP1)
  566. expectType<boolean | undefined>(this.aP2)
  567. expectType<number>(this.b)
  568. expectType<any>(this.bP1)
  569. expectType<number>(this.c)
  570. expectType<number>(this.d)
  571. expectType<number>(this.dC1)
  572. expectType<string>(this.dC2)
  573. // props should be readonly
  574. // @ts-expect-error
  575. this.aP1 = 'new'
  576. // @ts-expect-error
  577. this.z = 1
  578. // props on `this` should be readonly
  579. // @ts-expect-error
  580. this.bP1 = 1
  581. // string value can not assigned to number type value
  582. // @ts-expect-error
  583. this.c = '1'
  584. // setup context properties should be mutable
  585. this.d = 5
  586. return null
  587. }
  588. })
  589. // Test TSX
  590. expectType<JSX.Element>(
  591. <MyComponent aP1={'aP'} aP2 bP1={1} bP2={[1, 2]} z={'z'} />
  592. )
  593. // missing required props
  594. // @ts-expect-error
  595. ;<MyComponent />
  596. // wrong prop types
  597. // @ts-expect-error
  598. ;<MyComponent aP1="ap" aP2={'wrong type'} bP1="b" z={'z'} />
  599. // @ts-expect-error
  600. ;<MyComponent aP1={1} bP2={[1]} />
  601. })
  602. describe('with extends', () => {
  603. const Base = defineComponent({
  604. props: {
  605. aP1: Boolean,
  606. aP2: {
  607. type: Number,
  608. default: 2
  609. }
  610. },
  611. data() {
  612. return {
  613. a: 1
  614. }
  615. },
  616. computed: {
  617. c(): number {
  618. return this.aP2 + this.a
  619. }
  620. }
  621. })
  622. const MyComponent = defineComponent({
  623. extends: Base,
  624. props: {
  625. // required should make property non-void
  626. z: {
  627. type: String,
  628. required: true
  629. }
  630. },
  631. render() {
  632. const props = this.$props
  633. // props
  634. expectType<boolean | undefined>(props.aP1)
  635. expectType<number>(props.aP2)
  636. expectType<string>(props.z)
  637. const data = this.$data
  638. expectType<number>(data.a)
  639. // should also expose declared props on `this`
  640. expectType<number>(this.a)
  641. expectType<boolean | undefined>(this.aP1)
  642. expectType<number>(this.aP2)
  643. // setup context properties should be mutable
  644. this.a = 5
  645. return null
  646. }
  647. })
  648. // Test TSX
  649. expectType<JSX.Element>(<MyComponent aP2={3} aP1 z={'z'} />)
  650. // missing required props
  651. // @ts-expect-error
  652. ;<MyComponent />
  653. // wrong prop types
  654. // @ts-expect-error
  655. ;<MyComponent aP2={'wrong type'} z={'z'} />
  656. // @ts-expect-error
  657. ;<MyComponent aP1={3} />
  658. })
  659. describe('extends with mixins', () => {
  660. const Mixin = defineComponent({
  661. emits: ['bar'],
  662. props: {
  663. mP1: {
  664. type: String,
  665. default: 'mP1'
  666. },
  667. mP2: Boolean,
  668. mP3: {
  669. type: Boolean,
  670. required: true
  671. }
  672. },
  673. data() {
  674. return {
  675. a: 1
  676. }
  677. }
  678. })
  679. const Base = defineComponent({
  680. emits: ['foo'],
  681. props: {
  682. p1: Boolean,
  683. p2: {
  684. type: Number,
  685. default: 2
  686. },
  687. p3: {
  688. type: Boolean,
  689. required: true
  690. }
  691. },
  692. data() {
  693. return {
  694. b: 2
  695. }
  696. },
  697. computed: {
  698. c(): number {
  699. return this.p2 + this.b
  700. }
  701. }
  702. })
  703. const MyComponent = defineComponent({
  704. extends: Base,
  705. mixins: [Mixin],
  706. emits: ['click'],
  707. props: {
  708. // required should make property non-void
  709. z: {
  710. type: String,
  711. required: true
  712. }
  713. },
  714. render() {
  715. const props = this.$props
  716. // props
  717. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  718. // from Mixin
  719. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  720. // from Base
  721. expectType<((...args: any[]) => any) | undefined>(props.onFoo)
  722. expectType<boolean | undefined>(props.p1)
  723. expectType<number>(props.p2)
  724. expectType<string>(props.z)
  725. expectType<string>(props.mP1)
  726. expectType<boolean | undefined>(props.mP2)
  727. const data = this.$data
  728. expectType<number>(data.a)
  729. expectType<number>(data.b)
  730. // should also expose declared props on `this`
  731. expectType<number>(this.a)
  732. expectType<number>(this.b)
  733. expectType<boolean | undefined>(this.p1)
  734. expectType<number>(this.p2)
  735. expectType<string>(this.mP1)
  736. expectType<boolean | undefined>(this.mP2)
  737. // setup context properties should be mutable
  738. this.a = 5
  739. return null
  740. }
  741. })
  742. // Test TSX
  743. expectType<JSX.Element>(<MyComponent mP1="p1" mP2 mP3 p1 p2={1} p3 z={'z'} />)
  744. // mP1, mP2, p1, and p2 have default value. these are not required
  745. expectType<JSX.Element>(<MyComponent mP3 p3 z={'z'} />)
  746. // missing required props
  747. // @ts-expect-error
  748. ;<MyComponent mP3 p3 /* z='z' */ />
  749. // missing required props from mixin
  750. // @ts-expect-error
  751. ;<MyComponent /* mP3 */ p3 z="z" />
  752. // missing required props from extends
  753. // @ts-expect-error
  754. ;<MyComponent mP3 /* p3 */ z="z" />
  755. // wrong prop types
  756. // @ts-expect-error
  757. ;<MyComponent p2={'wrong type'} z={'z'} />
  758. // @ts-expect-error
  759. ;<MyComponent mP1={3} />
  760. // #3468
  761. const CompWithD = defineComponent({
  762. data() {
  763. return { foo: 1 }
  764. }
  765. })
  766. const CompWithC = defineComponent({
  767. computed: {
  768. foo() {
  769. return 1
  770. }
  771. }
  772. })
  773. const CompWithM = defineComponent({ methods: { foo() {} } })
  774. const CompEmpty = defineComponent({})
  775. defineComponent({
  776. mixins: [CompWithD, CompEmpty],
  777. mounted() {
  778. expectType<number>(this.foo)
  779. }
  780. })
  781. defineComponent({
  782. mixins: [CompWithC, CompEmpty],
  783. mounted() {
  784. expectType<number>(this.foo)
  785. }
  786. })
  787. defineComponent({
  788. mixins: [CompWithM, CompEmpty],
  789. mounted() {
  790. expectType<() => void>(this.foo)
  791. }
  792. })
  793. })
  794. describe('compatibility w/ createApp', () => {
  795. const comp = defineComponent({})
  796. createApp(comp).mount('#hello')
  797. const comp2 = defineComponent({
  798. props: { foo: String }
  799. })
  800. createApp(comp2).mount('#hello')
  801. const comp3 = defineComponent({
  802. setup() {
  803. return {
  804. a: 1
  805. }
  806. }
  807. })
  808. createApp(comp3).mount('#hello')
  809. })
  810. describe('defineComponent', () => {
  811. describe('should accept components defined with defineComponent', () => {
  812. const comp = defineComponent({})
  813. defineComponent({
  814. components: { comp }
  815. })
  816. })
  817. describe('should accept class components with receiving constructor arguments', () => {
  818. class Comp {
  819. static __vccOpts = {}
  820. constructor(_props: { foo: string }) {}
  821. }
  822. defineComponent({
  823. components: { Comp }
  824. })
  825. })
  826. })
  827. describe('emits', () => {
  828. // Note: for TSX inference, ideally we want to map emits to onXXX props,
  829. // but that requires type-level string constant concatenation as suggested in
  830. // https://github.com/Microsoft/TypeScript/issues/12754
  831. // The workaround for TSX users is instead of using emits, declare onXXX props
  832. // and call them instead. Since `v-on:click` compiles to an `onClick` prop,
  833. // this would also support other users consuming the component in templates
  834. // with `v-on` listeners.
  835. // with object emits
  836. defineComponent({
  837. emits: {
  838. click: (n: number) => typeof n === 'number',
  839. input: (b: string) => b.length > 1
  840. },
  841. setup(props, { emit }) {
  842. expectType<((n: number) => boolean) | undefined>(props.onClick)
  843. expectType<((b: string) => boolean) | undefined>(props.onInput)
  844. emit('click', 1)
  845. emit('input', 'foo')
  846. // @ts-expect-error
  847. emit('nope')
  848. // @ts-expect-error
  849. emit('click')
  850. // @ts-expect-error
  851. emit('click', 'foo')
  852. // @ts-expect-error
  853. emit('input')
  854. // @ts-expect-error
  855. emit('input', 1)
  856. },
  857. created() {
  858. this.$emit('click', 1)
  859. this.$emit('input', 'foo')
  860. // @ts-expect-error
  861. this.$emit('nope')
  862. // @ts-expect-error
  863. this.$emit('click')
  864. // @ts-expect-error
  865. this.$emit('click', 'foo')
  866. // @ts-expect-error
  867. this.$emit('input')
  868. // @ts-expect-error
  869. this.$emit('input', 1)
  870. },
  871. mounted() {
  872. // #3599
  873. this.$nextTick(function () {
  874. // this should be bound to this instance
  875. this.$emit('click', 1)
  876. this.$emit('input', 'foo')
  877. // @ts-expect-error
  878. this.$emit('nope')
  879. // @ts-expect-error
  880. this.$emit('click')
  881. // @ts-expect-error
  882. this.$emit('click', 'foo')
  883. // @ts-expect-error
  884. this.$emit('input')
  885. // @ts-expect-error
  886. this.$emit('input', 1)
  887. })
  888. }
  889. })
  890. // with array emits
  891. defineComponent({
  892. emits: ['foo', 'bar'],
  893. setup(props, { emit }) {
  894. expectType<((...args: any[]) => any) | undefined>(props.onFoo)
  895. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  896. emit('foo')
  897. emit('foo', 123)
  898. emit('bar')
  899. // @ts-expect-error
  900. emit('nope')
  901. },
  902. created() {
  903. this.$emit('foo')
  904. this.$emit('foo', 123)
  905. this.$emit('bar')
  906. // @ts-expect-error
  907. this.$emit('nope')
  908. }
  909. })
  910. // with tsx
  911. const Component = defineComponent({
  912. emits: {
  913. click: (n: number) => typeof n === 'number'
  914. },
  915. setup(props, { emit }) {
  916. expectType<((n: number) => any) | undefined>(props.onClick)
  917. emit('click', 1)
  918. // @ts-expect-error
  919. emit('click')
  920. // @ts-expect-error
  921. emit('click', 'foo')
  922. }
  923. })
  924. defineComponent({
  925. render() {
  926. return (
  927. <Component
  928. onClick={(n: number) => {
  929. return n + 1
  930. }}
  931. />
  932. )
  933. }
  934. })
  935. // without emits
  936. defineComponent({
  937. setup(props, { emit }) {
  938. emit('test', 1)
  939. emit('test')
  940. }
  941. })
  942. // emit should be valid when ComponentPublicInstance is used.
  943. const instance = {} as ComponentPublicInstance
  944. instance.$emit('test', 1)
  945. instance.$emit('test')
  946. // `this` should be void inside of emits validators
  947. defineComponent({
  948. props: ['bar'],
  949. emits: {
  950. foo(): boolean {
  951. // @ts-expect-error
  952. return this.bar === 3
  953. }
  954. }
  955. })
  956. })
  957. describe('inject', () => {
  958. // with object inject
  959. defineComponent({
  960. props: {
  961. a: String
  962. },
  963. inject: {
  964. foo: 'foo',
  965. bar: 'bar'
  966. },
  967. created() {
  968. expectType<unknown>(this.foo)
  969. expectType<unknown>(this.bar)
  970. // @ts-expect-error
  971. this.foobar = 1
  972. }
  973. })
  974. // with array inject
  975. defineComponent({
  976. props: ['a', 'b'],
  977. inject: ['foo', 'bar'],
  978. created() {
  979. expectType<unknown>(this.foo)
  980. expectType<unknown>(this.bar)
  981. // @ts-expect-error
  982. this.foobar = 1
  983. }
  984. })
  985. // with no props
  986. defineComponent({
  987. inject: {
  988. foo: {
  989. from: 'pfoo',
  990. default: 'foo'
  991. },
  992. bar: {
  993. from: 'pbar',
  994. default: 'bar'
  995. }
  996. },
  997. created() {
  998. expectType<unknown>(this.foo)
  999. expectType<unknown>(this.bar)
  1000. // @ts-expect-error
  1001. this.foobar = 1
  1002. }
  1003. })
  1004. // without inject
  1005. defineComponent({
  1006. props: ['a', 'b'],
  1007. created() {
  1008. // @ts-expect-error
  1009. this.foo = 1
  1010. // @ts-expect-error
  1011. this.bar = 1
  1012. }
  1013. })
  1014. })
  1015. describe('componentOptions setup should be `SetupContext`', () => {
  1016. expectType<ComponentOptions['setup']>(
  1017. {} as (props: Record<string, any>, ctx: SetupContext) => any
  1018. )
  1019. })
  1020. describe('extract instance type', () => {
  1021. const Base = defineComponent({
  1022. props: {
  1023. baseA: {
  1024. type: Number,
  1025. default: 1
  1026. }
  1027. }
  1028. })
  1029. const MixinA = defineComponent({
  1030. props: {
  1031. mA: {
  1032. type: String,
  1033. default: ''
  1034. }
  1035. }
  1036. })
  1037. const CompA = defineComponent({
  1038. extends: Base,
  1039. mixins: [MixinA],
  1040. props: {
  1041. a: {
  1042. type: Boolean,
  1043. default: false
  1044. },
  1045. b: {
  1046. type: String,
  1047. required: true
  1048. },
  1049. c: Number
  1050. }
  1051. })
  1052. const compA = {} as InstanceType<typeof CompA>
  1053. expectType<boolean>(compA.a)
  1054. expectType<string>(compA.b)
  1055. expectType<number | undefined>(compA.c)
  1056. // mixins
  1057. expectType<string>(compA.mA)
  1058. // extends
  1059. expectType<number>(compA.baseA)
  1060. // @ts-expect-error
  1061. compA.a = true
  1062. // @ts-expect-error
  1063. compA.b = 'foo'
  1064. // @ts-expect-error
  1065. compA.c = 1
  1066. // @ts-expect-error
  1067. compA.mA = 'foo'
  1068. // @ts-expect-error
  1069. compA.baseA = 1
  1070. })
  1071. describe('async setup', () => {
  1072. type GT = string & { __brand: unknown }
  1073. const Comp = defineComponent({
  1074. async setup() {
  1075. // setup context
  1076. return {
  1077. a: ref(1),
  1078. b: {
  1079. c: ref('hi')
  1080. },
  1081. d: reactive({
  1082. e: ref('hello' as GT)
  1083. })
  1084. }
  1085. },
  1086. render() {
  1087. // assert setup context unwrapping
  1088. expectType<number>(this.a)
  1089. expectType<string>(this.b.c.value)
  1090. expectType<GT>(this.d.e)
  1091. // setup context properties should be mutable
  1092. this.a = 2
  1093. }
  1094. })
  1095. const vm = {} as InstanceType<typeof Comp>
  1096. // assert setup context unwrapping
  1097. expectType<number>(vm.a)
  1098. expectType<string>(vm.b.c.value)
  1099. expectType<GT>(vm.d.e)
  1100. // setup context properties should be mutable
  1101. vm.a = 2
  1102. })
  1103. // #5948
  1104. describe('DefineComponent should infer correct types when assigning to Component', () => {
  1105. let component: Component
  1106. component = defineComponent({
  1107. setup(_, { attrs, slots }) {
  1108. // @ts-expect-error should not be any
  1109. expectType<[]>(attrs)
  1110. // @ts-expect-error should not be any
  1111. expectType<[]>(slots)
  1112. }
  1113. })
  1114. expectType<Component>(component)
  1115. })
  1116. // #5969
  1117. describe('should allow to assign props', () => {
  1118. const Child = defineComponent({
  1119. props: {
  1120. bar: String
  1121. }
  1122. })
  1123. const Parent = defineComponent({
  1124. props: {
  1125. ...Child.props,
  1126. foo: String
  1127. }
  1128. })
  1129. const child = new Child()
  1130. expectType<JSX.Element>(<Parent {...child.$props} />)
  1131. })
  1132. // #6052
  1133. describe('prop starting with `on*` is broken', () => {
  1134. defineComponent({
  1135. props: {
  1136. onX: {
  1137. type: Function as PropType<(a: 1) => void>,
  1138. required: true
  1139. }
  1140. },
  1141. setup(props) {
  1142. expectType<(a: 1) => void>(props.onX)
  1143. props.onX(1)
  1144. }
  1145. })
  1146. defineComponent({
  1147. props: {
  1148. onX: {
  1149. type: Function as PropType<(a: 1) => void>,
  1150. required: true
  1151. }
  1152. },
  1153. emits: {
  1154. test: (a: 1) => true
  1155. },
  1156. setup(props) {
  1157. expectType<(a: 1) => void>(props.onX)
  1158. expectType<undefined | ((a: 1) => any)>(props.onTest)
  1159. }
  1160. })
  1161. })
  1162. describe('function syntax w/ generics', () => {
  1163. const Comp = defineComponent(
  1164. // TODO: babel plugin to auto infer runtime props options from type
  1165. // similar to defineProps<{...}>()
  1166. <T extends string | number>(props: { msg: T; list: T[] }) => {
  1167. // use Composition API here like in <script setup>
  1168. const count = ref(0)
  1169. return () => (
  1170. // return a render function (both JSX and h() works)
  1171. <div>
  1172. {props.msg} {count.value}
  1173. </div>
  1174. )
  1175. }
  1176. )
  1177. expectType<JSX.Element>(<Comp msg="fse" list={['foo']} />)
  1178. expectType<JSX.Element>(<Comp msg={123} list={[123]} />)
  1179. expectType<JSX.Element>(
  1180. // @ts-expect-error missing prop
  1181. <Comp msg={123} />
  1182. )
  1183. expectType<JSX.Element>(
  1184. // @ts-expect-error generics don't match
  1185. <Comp msg="fse" list={[123]} />
  1186. )
  1187. expectType<JSX.Element>(
  1188. // @ts-expect-error generics don't match
  1189. <Comp msg={123} list={['123']} />
  1190. )
  1191. })
  1192. describe('function syntax w/ emits', () => {
  1193. const Foo = defineComponent(
  1194. (props: { msg: string }, ctx) => {
  1195. ctx.emit('foo')
  1196. // @ts-expect-error
  1197. ctx.emit('bar')
  1198. return () => {}
  1199. },
  1200. {
  1201. emits: ['foo']
  1202. }
  1203. )
  1204. expectType<JSX.Element>(<Foo msg="hi" onFoo={() => {}} />)
  1205. // @ts-expect-error
  1206. expectType<JSX.Element>(<Foo msg="hi" onBar={() => {}} />)
  1207. defineComponent(
  1208. (props: { msg: string }, ctx) => {
  1209. ctx.emit('foo', 'hi')
  1210. // @ts-expect-error
  1211. ctx.emit('foo')
  1212. // @ts-expect-error
  1213. ctx.emit('bar')
  1214. return () => {}
  1215. },
  1216. {
  1217. emits: {
  1218. foo: (a: string) => true
  1219. }
  1220. }
  1221. )
  1222. })
  1223. describe('function syntax w/ runtime props', () => {
  1224. // with runtime props, the runtime props must match
  1225. // manual type declaration
  1226. defineComponent(
  1227. (_props: { msg: string }) => {
  1228. return () => {}
  1229. },
  1230. {
  1231. props: ['msg']
  1232. }
  1233. )
  1234. defineComponent(
  1235. <T extends string>(_props: { msg: T }) => {
  1236. return () => {}
  1237. },
  1238. {
  1239. props: ['msg']
  1240. }
  1241. )
  1242. defineComponent(
  1243. <T extends string>(_props: { msg: T }) => {
  1244. return () => {}
  1245. },
  1246. {
  1247. props: {
  1248. msg: String
  1249. }
  1250. }
  1251. )
  1252. // @ts-expect-error string prop names don't match
  1253. defineComponent(
  1254. (_props: { msg: string }) => {
  1255. return () => {}
  1256. },
  1257. {
  1258. props: ['bar']
  1259. }
  1260. )
  1261. // @ts-expect-error prop type mismatch
  1262. defineComponent(
  1263. (_props: { msg: string }) => {
  1264. return () => {}
  1265. },
  1266. {
  1267. props: {
  1268. msg: Number
  1269. }
  1270. }
  1271. )
  1272. // @ts-expect-error prop keys don't match
  1273. defineComponent(
  1274. (_props: { msg: string }, ctx) => {
  1275. return () => {}
  1276. },
  1277. {
  1278. props: {
  1279. msg: String,
  1280. bar: String
  1281. }
  1282. }
  1283. )
  1284. })
  1285. // check if defineComponent can be exported
  1286. export default {
  1287. // function components
  1288. a: defineComponent(_ => () => h('div')),
  1289. // no props
  1290. b: defineComponent({
  1291. data() {
  1292. return {}
  1293. }
  1294. }),
  1295. c: defineComponent({
  1296. props: ['a']
  1297. }),
  1298. d: defineComponent({
  1299. props: {
  1300. a: Number
  1301. }
  1302. })
  1303. }
  1304. describe('slots', () => {
  1305. const comp1 = defineComponent({
  1306. slots: Object as SlotsType<{
  1307. default: { foo: string; bar: number }
  1308. optional?: { data: string }
  1309. undefinedScope: undefined | { data: string }
  1310. optionalUndefinedScope?: undefined | { data: string }
  1311. }>,
  1312. setup(props, { slots }) {
  1313. expectType<(scope: { foo: string; bar: number }) => VNode[]>(
  1314. slots.default
  1315. )
  1316. expectType<((scope: { data: string }) => VNode[]) | undefined>(
  1317. slots.optional
  1318. )
  1319. slots.default({ foo: 'foo', bar: 1 })
  1320. // @ts-expect-error it's optional
  1321. slots.optional({ data: 'foo' })
  1322. slots.optional?.({ data: 'foo' })
  1323. expectType<{
  1324. (): VNode[]
  1325. (scope: undefined | { data: string }): VNode[]
  1326. }>(slots.undefinedScope)
  1327. expectType<
  1328. | { (): VNode[]; (scope: undefined | { data: string }): VNode[] }
  1329. | undefined
  1330. >(slots.optionalUndefinedScope)
  1331. slots.default({ foo: 'foo', bar: 1 })
  1332. // @ts-expect-error it's optional
  1333. slots.optional({ data: 'foo' })
  1334. slots.optional?.({ data: 'foo' })
  1335. slots.undefinedScope()
  1336. slots.undefinedScope(undefined)
  1337. // @ts-expect-error
  1338. slots.undefinedScope('foo')
  1339. slots.optionalUndefinedScope?.()
  1340. slots.optionalUndefinedScope?.(undefined)
  1341. slots.optionalUndefinedScope?.({ data: 'foo' })
  1342. // @ts-expect-error
  1343. slots.optionalUndefinedScope()
  1344. // @ts-expect-error
  1345. slots.optionalUndefinedScope?.('foo')
  1346. expectType<typeof slots | undefined>(new comp1().$slots)
  1347. }
  1348. })
  1349. const comp2 = defineComponent({
  1350. setup(props, { slots }) {
  1351. // unknown slots
  1352. expectType<Slots>(slots)
  1353. expectType<((...args: any[]) => VNode[]) | undefined>(slots.default)
  1354. }
  1355. })
  1356. expectType<Slots | undefined>(new comp2().$slots)
  1357. })
  1358. import {
  1359. DefineComponent,
  1360. ComponentOptionsMixin,
  1361. EmitsOptions,
  1362. VNodeProps,
  1363. AllowedComponentProps,
  1364. ComponentCustomProps,
  1365. ExtractPropTypes
  1366. } from 'vue'
  1367. // code generated by tsc / vue-tsc, make sure this continues to work
  1368. // so we don't accidentally change the args order of DefineComponent
  1369. declare const MyButton: DefineComponent<
  1370. {},
  1371. () => JSX.Element,
  1372. {},
  1373. {},
  1374. {},
  1375. ComponentOptionsMixin,
  1376. ComponentOptionsMixin,
  1377. EmitsOptions,
  1378. string,
  1379. VNodeProps & AllowedComponentProps & ComponentCustomProps,
  1380. Readonly<ExtractPropTypes<{}>>,
  1381. {},
  1382. {}
  1383. >
  1384. ;<MyButton class="x" />