defineComponent.test-d.tsx 33 KB

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