defineComponent.test-d.tsx 29 KB

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