defineComponent.test-d.tsx 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. expectType<typeof props>(MyComponent.props)
  254. // @ts-expect-error it should be an object and not any
  255. expectError<[]>(MyComponent.props)
  256. expectType<() => {}>(MyComponent.provide)
  257. // @ts-expect-error
  258. expectError<[]>(MyComponent.provide)
  259. expectType<() => null>(MyComponent.render)
  260. expectType<Function | undefined>(defineComponent({}).render)
  261. // Test TSX
  262. expectType<JSX.Element>(
  263. <MyComponent
  264. a={1}
  265. b="b"
  266. bb="bb"
  267. e={() => {}}
  268. cc={['cc']}
  269. dd={{ n: 1 }}
  270. ee={() => 'ee'}
  271. ccc={['ccc']}
  272. ddd={['ddd']}
  273. eee={() => ({ a: 'eee' })}
  274. fff={(a, b) => ({ a: a > +b })}
  275. hhh={false}
  276. ggg="foo"
  277. jjj={() => ''}
  278. // should allow class/style as attrs
  279. class="bar"
  280. style={{ color: 'red' }}
  281. // should allow key
  282. key={'foo'}
  283. // should allow ref
  284. ref={'foo'}
  285. ref_for={true}
  286. />
  287. )
  288. expectType<Component>(
  289. <MyComponent
  290. b="b"
  291. dd={{ n: 1 }}
  292. ddd={['ddd']}
  293. eee={() => ({ a: 'eee' })}
  294. fff={(a, b) => ({ a: a > +b })}
  295. hhh={false}
  296. jjj={() => ''}
  297. />
  298. )
  299. // @ts-expect-error missing required props
  300. expectError(<MyComponent />)
  301. expectError(
  302. // @ts-expect-error wrong prop types
  303. <MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
  304. )
  305. expectError(
  306. // @ts-expect-error wrong prop types
  307. <MyComponent ggg="baz" />
  308. )
  309. // @ts-expect-error
  310. expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
  311. // `this` should be void inside of prop validators and prop default factories
  312. defineComponent({
  313. props: {
  314. myProp: {
  315. type: Number,
  316. validator(val: unknown): boolean {
  317. // @ts-expect-error
  318. return val !== this.otherProp
  319. },
  320. default(): number {
  321. // @ts-expect-error
  322. return this.otherProp + 1
  323. }
  324. },
  325. otherProp: {
  326. type: Number,
  327. required: true
  328. }
  329. }
  330. })
  331. })
  332. describe('type inference w/ optional props declaration', () => {
  333. const MyComponent = defineComponent<{ a: string[]; msg: string }>({
  334. setup(props) {
  335. expectType<string>(props.msg)
  336. expectType<string[]>(props.a)
  337. return {
  338. b: 1
  339. }
  340. }
  341. })
  342. expectType<JSX.Element>(<MyComponent msg="1" a={['1']} />)
  343. // @ts-expect-error
  344. expectError(<MyComponent />)
  345. // @ts-expect-error
  346. expectError(<MyComponent msg="1" />)
  347. })
  348. describe('type inference w/ direct setup function', () => {
  349. const MyComponent = defineComponent((_props: { msg: string }) => {})
  350. expectType<JSX.Element>(<MyComponent msg="foo" />)
  351. // @ts-expect-error
  352. expectError(<MyComponent />)
  353. expectError(<MyComponent msg="1" />)
  354. })
  355. describe('type inference w/ array props declaration', () => {
  356. const MyComponent = defineComponent({
  357. props: ['a', 'b'],
  358. setup(props) {
  359. // @ts-expect-error props should be readonly
  360. expectError((props.a = 1))
  361. expectType<any>(props.a)
  362. expectType<any>(props.b)
  363. return {
  364. c: 1
  365. }
  366. },
  367. render() {
  368. expectType<any>(this.$props.a)
  369. expectType<any>(this.$props.b)
  370. // @ts-expect-error
  371. expectError((this.$props.a = 1))
  372. expectType<any>(this.a)
  373. expectType<any>(this.b)
  374. expectType<number>(this.c)
  375. }
  376. })
  377. expectType<JSX.Element>(<MyComponent a={[1, 2]} b="b" />)
  378. // @ts-expect-error
  379. expectError(<MyComponent other="other" />)
  380. })
  381. describe('type inference w/ options API', () => {
  382. defineComponent({
  383. props: { a: Number },
  384. setup() {
  385. return {
  386. b: 123
  387. }
  388. },
  389. data() {
  390. // Limitation: we cannot expose the return result of setup() on `this`
  391. // here in data() - somehow that would mess up the inference
  392. expectType<number | undefined>(this.a)
  393. return {
  394. c: this.a || 123,
  395. someRef: ref(0)
  396. }
  397. },
  398. computed: {
  399. d() {
  400. expectType<number>(this.b)
  401. return this.b + 1
  402. },
  403. e: {
  404. get() {
  405. expectType<number>(this.b)
  406. expectType<number>(this.d)
  407. return this.b + this.d
  408. },
  409. set(v: number) {
  410. expectType<number>(this.b)
  411. expectType<number>(this.d)
  412. expectType<number>(v)
  413. }
  414. }
  415. },
  416. watch: {
  417. a() {
  418. expectType<number>(this.b)
  419. this.b + 1
  420. }
  421. },
  422. created() {
  423. // props
  424. expectType<number | undefined>(this.a)
  425. // returned from setup()
  426. expectType<number>(this.b)
  427. // returned from data()
  428. expectType<number>(this.c)
  429. // computed
  430. expectType<number>(this.d)
  431. // computed get/set
  432. expectType<number>(this.e)
  433. expectType<number>(this.someRef)
  434. },
  435. methods: {
  436. doSomething() {
  437. // props
  438. expectType<number | undefined>(this.a)
  439. // returned from setup()
  440. expectType<number>(this.b)
  441. // returned from data()
  442. expectType<number>(this.c)
  443. // computed
  444. expectType<number>(this.d)
  445. // computed get/set
  446. expectType<number>(this.e)
  447. },
  448. returnSomething() {
  449. return this.a
  450. }
  451. },
  452. render() {
  453. // props
  454. expectType<number | undefined>(this.a)
  455. // returned from setup()
  456. expectType<number>(this.b)
  457. // returned from data()
  458. expectType<number>(this.c)
  459. // computed
  460. expectType<number>(this.d)
  461. // computed get/set
  462. expectType<number>(this.e)
  463. // method
  464. expectType<() => number | undefined>(this.returnSomething)
  465. }
  466. })
  467. })
  468. describe('with mixins', () => {
  469. const MixinA = defineComponent({
  470. emits: ['bar'],
  471. props: {
  472. aP1: {
  473. type: String,
  474. default: 'aP1'
  475. },
  476. aP2: Boolean
  477. },
  478. data() {
  479. return {
  480. a: 1
  481. }
  482. }
  483. })
  484. const MixinB = defineComponent({
  485. props: ['bP1', 'bP2'],
  486. data() {
  487. return {
  488. b: 2
  489. }
  490. }
  491. })
  492. const MixinC = defineComponent({
  493. data() {
  494. return {
  495. c: 3
  496. }
  497. }
  498. })
  499. const MixinD = defineComponent({
  500. mixins: [MixinA],
  501. data() {
  502. //@ts-expect-error computed are not available on data()
  503. expectError<number>(this.dC1)
  504. //@ts-expect-error computed are not available on data()
  505. expectError<string>(this.dC2)
  506. return {
  507. d: 4
  508. }
  509. },
  510. setup(props) {
  511. expectType<string>(props.aP1)
  512. },
  513. computed: {
  514. dC1() {
  515. return this.d + this.a
  516. },
  517. dC2() {
  518. return this.aP1 + 'dC2'
  519. }
  520. }
  521. })
  522. const MyComponent = defineComponent({
  523. mixins: [MixinA, MixinB, MixinC, MixinD],
  524. emits: ['click'],
  525. props: {
  526. // required should make property non-void
  527. z: {
  528. type: String,
  529. required: true
  530. }
  531. },
  532. data(vm) {
  533. expectType<number>(vm.a)
  534. expectType<number>(vm.b)
  535. expectType<number>(vm.c)
  536. expectType<number>(vm.d)
  537. // should also expose declared props on `this`
  538. expectType<number>(this.a)
  539. expectType<string>(this.aP1)
  540. expectType<boolean | undefined>(this.aP2)
  541. expectType<number>(this.b)
  542. expectType<any>(this.bP1)
  543. expectType<number>(this.c)
  544. expectType<number>(this.d)
  545. return {}
  546. },
  547. setup(props) {
  548. expectType<string>(props.z)
  549. // props
  550. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  551. // from Base
  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. },
  559. render() {
  560. const props = this.$props
  561. // props
  562. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  563. // from Base
  564. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  565. expectType<string>(props.aP1)
  566. expectType<boolean | undefined>(props.aP2)
  567. expectType<any>(props.bP1)
  568. expectType<any>(props.bP2)
  569. expectType<string>(props.z)
  570. const data = this.$data
  571. expectType<number>(data.a)
  572. expectType<number>(data.b)
  573. expectType<number>(data.c)
  574. expectType<number>(data.d)
  575. // should also expose declared props on `this`
  576. expectType<number>(this.a)
  577. expectType<string>(this.aP1)
  578. expectType<boolean | undefined>(this.aP2)
  579. expectType<number>(this.b)
  580. expectType<any>(this.bP1)
  581. expectType<number>(this.c)
  582. expectType<number>(this.d)
  583. expectType<number>(this.dC1)
  584. expectType<string>(this.dC2)
  585. // props should be readonly
  586. // @ts-expect-error
  587. expectError((this.aP1 = 'new'))
  588. // @ts-expect-error
  589. expectError((this.z = 1))
  590. // props on `this` should be readonly
  591. // @ts-expect-error
  592. expectError((this.bP1 = 1))
  593. // string value can not assigned to number type value
  594. // @ts-expect-error
  595. expectError((this.c = '1'))
  596. // setup context properties should be mutable
  597. this.d = 5
  598. return null
  599. }
  600. })
  601. // Test TSX
  602. expectType<JSX.Element>(
  603. <MyComponent aP1={'aP'} aP2 bP1={1} bP2={[1, 2]} z={'z'} />
  604. )
  605. // missing required props
  606. // @ts-expect-error
  607. expectError(<MyComponent />)
  608. // wrong prop types
  609. // @ts-expect-error
  610. expectError(<MyComponent aP1="ap" aP2={'wrong type'} bP1="b" z={'z'} />)
  611. // @ts-expect-error
  612. expectError(<MyComponent aP1={1} bP2={[1]} />)
  613. })
  614. describe('with extends', () => {
  615. const Base = defineComponent({
  616. props: {
  617. aP1: Boolean,
  618. aP2: {
  619. type: Number,
  620. default: 2
  621. }
  622. },
  623. data() {
  624. return {
  625. a: 1
  626. }
  627. },
  628. computed: {
  629. c(): number {
  630. return this.aP2 + this.a
  631. }
  632. }
  633. })
  634. const MyComponent = defineComponent({
  635. extends: Base,
  636. props: {
  637. // required should make property non-void
  638. z: {
  639. type: String,
  640. required: true
  641. }
  642. },
  643. render() {
  644. const props = this.$props
  645. // props
  646. expectType<boolean | undefined>(props.aP1)
  647. expectType<number>(props.aP2)
  648. expectType<string>(props.z)
  649. const data = this.$data
  650. expectType<number>(data.a)
  651. // should also expose declared props on `this`
  652. expectType<number>(this.a)
  653. expectType<boolean | undefined>(this.aP1)
  654. expectType<number>(this.aP2)
  655. // setup context properties should be mutable
  656. this.a = 5
  657. return null
  658. }
  659. })
  660. // Test TSX
  661. expectType<JSX.Element>(<MyComponent aP2={3} aP1 z={'z'} />)
  662. // missing required props
  663. // @ts-expect-error
  664. expectError(<MyComponent />)
  665. // wrong prop types
  666. // @ts-expect-error
  667. expectError(<MyComponent aP2={'wrong type'} z={'z'} />)
  668. // @ts-expect-error
  669. expectError(<MyComponent aP1={3} />)
  670. })
  671. describe('extends with mixins', () => {
  672. const Mixin = defineComponent({
  673. emits: ['bar'],
  674. props: {
  675. mP1: {
  676. type: String,
  677. default: 'mP1'
  678. },
  679. mP2: Boolean,
  680. mP3: {
  681. type: Boolean,
  682. required: true
  683. }
  684. },
  685. data() {
  686. return {
  687. a: 1
  688. }
  689. }
  690. })
  691. const Base = defineComponent({
  692. emits: ['foo'],
  693. props: {
  694. p1: Boolean,
  695. p2: {
  696. type: Number,
  697. default: 2
  698. },
  699. p3: {
  700. type: Boolean,
  701. required: true
  702. }
  703. },
  704. data() {
  705. return {
  706. b: 2
  707. }
  708. },
  709. computed: {
  710. c(): number {
  711. return this.p2 + this.b
  712. }
  713. }
  714. })
  715. const MyComponent = defineComponent({
  716. extends: Base,
  717. mixins: [Mixin],
  718. emits: ['click'],
  719. props: {
  720. // required should make property non-void
  721. z: {
  722. type: String,
  723. required: true
  724. }
  725. },
  726. render() {
  727. const props = this.$props
  728. // props
  729. expectType<((...args: any[]) => any) | undefined>(props.onClick)
  730. // from Mixin
  731. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  732. // from Base
  733. expectType<((...args: any[]) => any) | undefined>(props.onFoo)
  734. expectType<boolean | undefined>(props.p1)
  735. expectType<number>(props.p2)
  736. expectType<string>(props.z)
  737. expectType<string>(props.mP1)
  738. expectType<boolean | undefined>(props.mP2)
  739. const data = this.$data
  740. expectType<number>(data.a)
  741. expectType<number>(data.b)
  742. // should also expose declared props on `this`
  743. expectType<number>(this.a)
  744. expectType<number>(this.b)
  745. expectType<boolean | undefined>(this.p1)
  746. expectType<number>(this.p2)
  747. expectType<string>(this.mP1)
  748. expectType<boolean | undefined>(this.mP2)
  749. // setup context properties should be mutable
  750. this.a = 5
  751. return null
  752. }
  753. })
  754. // Test TSX
  755. expectType<JSX.Element>(<MyComponent mP1="p1" mP2 mP3 p1 p2={1} p3 z={'z'} />)
  756. // mP1, mP2, p1, and p2 have default value. these are not required
  757. expectType<JSX.Element>(<MyComponent mP3 p3 z={'z'} />)
  758. // missing required props
  759. // @ts-expect-error
  760. expectError(<MyComponent mP3 p3 /* z='z' */ />)
  761. // missing required props from mixin
  762. // @ts-expect-error
  763. expectError(<MyComponent /* mP3 */ p3 z="z" />)
  764. // missing required props from extends
  765. // @ts-expect-error
  766. expectError(<MyComponent mP3 /* p3 */ z="z" />)
  767. // wrong prop types
  768. // @ts-expect-error
  769. expectError(<MyComponent p2={'wrong type'} z={'z'} />)
  770. // @ts-expect-error
  771. expectError(<MyComponent mP1={3} />)
  772. // #3468
  773. const CompWithD = defineComponent({
  774. data() {
  775. return { foo: 1 }
  776. }
  777. })
  778. const CompWithC = defineComponent({
  779. computed: {
  780. foo() {
  781. return 1
  782. }
  783. }
  784. })
  785. const CompWithM = defineComponent({ methods: { foo() {} } })
  786. const CompEmpty = defineComponent({})
  787. defineComponent({
  788. mixins: [CompWithD, CompEmpty],
  789. mounted() {
  790. expectType<number>(this.foo)
  791. }
  792. })
  793. defineComponent({
  794. mixins: [CompWithC, CompEmpty],
  795. mounted() {
  796. expectType<number>(this.foo)
  797. }
  798. })
  799. defineComponent({
  800. mixins: [CompWithM, CompEmpty],
  801. mounted() {
  802. expectType<() => void>(this.foo)
  803. }
  804. })
  805. })
  806. describe('compatibility w/ createApp', () => {
  807. const comp = defineComponent({})
  808. createApp(comp).mount('#hello')
  809. const comp2 = defineComponent({
  810. props: { foo: String }
  811. })
  812. createApp(comp2).mount('#hello')
  813. const comp3 = defineComponent({
  814. setup() {
  815. return {
  816. a: 1
  817. }
  818. }
  819. })
  820. createApp(comp3).mount('#hello')
  821. })
  822. describe('defineComponent', () => {
  823. test('should accept components defined with defineComponent', () => {
  824. const comp = defineComponent({})
  825. defineComponent({
  826. components: { comp }
  827. })
  828. })
  829. test('should accept class components with receiving constructor arguments', () => {
  830. class Comp {
  831. static __vccOpts = {}
  832. constructor(_props: { foo: string }) {}
  833. }
  834. defineComponent({
  835. components: { Comp }
  836. })
  837. })
  838. })
  839. describe('emits', () => {
  840. // Note: for TSX inference, ideally we want to map emits to onXXX props,
  841. // but that requires type-level string constant concatenation as suggested in
  842. // https://github.com/Microsoft/TypeScript/issues/12754
  843. // The workaround for TSX users is instead of using emits, declare onXXX props
  844. // and call them instead. Since `v-on:click` compiles to an `onClick` prop,
  845. // this would also support other users consuming the component in templates
  846. // with `v-on` listeners.
  847. // with object emits
  848. defineComponent({
  849. emits: {
  850. click: (n: number) => typeof n === 'number',
  851. input: (b: string) => b.length > 1
  852. },
  853. setup(props, { emit }) {
  854. expectType<((n: number) => boolean) | undefined>(props.onClick)
  855. expectType<((b: string) => boolean) | undefined>(props.onInput)
  856. emit('click', 1)
  857. emit('input', 'foo')
  858. // @ts-expect-error
  859. expectError(emit('nope'))
  860. // @ts-expect-error
  861. expectError(emit('click'))
  862. // @ts-expect-error
  863. expectError(emit('click', 'foo'))
  864. // @ts-expect-error
  865. expectError(emit('input'))
  866. // @ts-expect-error
  867. expectError(emit('input', 1))
  868. },
  869. created() {
  870. this.$emit('click', 1)
  871. this.$emit('input', 'foo')
  872. // @ts-expect-error
  873. expectError(this.$emit('nope'))
  874. // @ts-expect-error
  875. expectError(this.$emit('click'))
  876. // @ts-expect-error
  877. expectError(this.$emit('click', 'foo'))
  878. // @ts-expect-error
  879. expectError(this.$emit('input'))
  880. // @ts-expect-error
  881. expectError(this.$emit('input', 1))
  882. },
  883. mounted() {
  884. // #3599
  885. this.$nextTick(function () {
  886. // this should be bound to this instance
  887. this.$emit('click', 1)
  888. this.$emit('input', 'foo')
  889. // @ts-expect-error
  890. expectError(this.$emit('nope'))
  891. // @ts-expect-error
  892. expectError(this.$emit('click'))
  893. // @ts-expect-error
  894. expectError(this.$emit('click', 'foo'))
  895. // @ts-expect-error
  896. expectError(this.$emit('input'))
  897. // @ts-expect-error
  898. expectError(this.$emit('input', 1))
  899. })
  900. }
  901. })
  902. // with array emits
  903. defineComponent({
  904. emits: ['foo', 'bar'],
  905. setup(props, { emit }) {
  906. expectType<((...args: any[]) => any) | undefined>(props.onFoo)
  907. expectType<((...args: any[]) => any) | undefined>(props.onBar)
  908. emit('foo')
  909. emit('foo', 123)
  910. emit('bar')
  911. // @ts-expect-error
  912. expectError(emit('nope'))
  913. },
  914. created() {
  915. this.$emit('foo')
  916. this.$emit('foo', 123)
  917. this.$emit('bar')
  918. // @ts-expect-error
  919. expectError(this.$emit('nope'))
  920. }
  921. })
  922. // with tsx
  923. const Component = defineComponent({
  924. emits: {
  925. click: (n: number) => typeof n === 'number'
  926. },
  927. setup(props, { emit }) {
  928. expectType<((n: number) => any) | undefined>(props.onClick)
  929. emit('click', 1)
  930. // @ts-expect-error
  931. expectError(emit('click'))
  932. // @ts-expect-error
  933. expectError(emit('click', 'foo'))
  934. }
  935. })
  936. defineComponent({
  937. render() {
  938. return (
  939. <Component
  940. onClick={(n: number) => {
  941. return n + 1
  942. }}
  943. />
  944. )
  945. }
  946. })
  947. // without emits
  948. defineComponent({
  949. setup(props, { emit }) {
  950. emit('test', 1)
  951. emit('test')
  952. }
  953. })
  954. // emit should be valid when ComponentPublicInstance is used.
  955. const instance = {} as ComponentPublicInstance
  956. instance.$emit('test', 1)
  957. instance.$emit('test')
  958. // `this` should be void inside of emits validators
  959. defineComponent({
  960. props: ['bar'],
  961. emits: {
  962. foo(): boolean {
  963. // @ts-expect-error
  964. return this.bar === 3
  965. }
  966. }
  967. })
  968. })
  969. describe('componentOptions setup should be `SetupContext`', () => {
  970. expectType<ComponentOptions['setup']>(
  971. {} as (props: Record<string, any>, ctx: SetupContext) => any
  972. )
  973. })
  974. describe('extract instance type', () => {
  975. const Base = defineComponent({
  976. props: {
  977. baseA: {
  978. type: Number,
  979. default: 1
  980. }
  981. }
  982. })
  983. const MixinA = defineComponent({
  984. props: {
  985. mA: {
  986. type: String,
  987. default: ''
  988. }
  989. }
  990. })
  991. const CompA = defineComponent({
  992. extends: Base,
  993. mixins: [MixinA],
  994. props: {
  995. a: {
  996. type: Boolean,
  997. default: false
  998. },
  999. b: {
  1000. type: String,
  1001. required: true
  1002. },
  1003. c: Number
  1004. }
  1005. })
  1006. const compA = {} as InstanceType<typeof CompA>
  1007. expectType<boolean>(compA.a)
  1008. expectType<string>(compA.b)
  1009. expectType<number | undefined>(compA.c)
  1010. // mixins
  1011. expectType<string>(compA.mA)
  1012. // extends
  1013. expectType<number>(compA.baseA)
  1014. // @ts-expect-error
  1015. expectError((compA.a = true))
  1016. // @ts-expect-error
  1017. expectError((compA.b = 'foo'))
  1018. // @ts-expect-error
  1019. expectError((compA.c = 1))
  1020. // @ts-expect-error
  1021. expectError((compA.mA = 'foo'))
  1022. // @ts-expect-error
  1023. expectError((compA.baseA = 1))
  1024. })
  1025. describe('async setup', () => {
  1026. type GT = string & { __brand: unknown }
  1027. const Comp = defineComponent({
  1028. async setup() {
  1029. // setup context
  1030. return {
  1031. a: ref(1),
  1032. b: {
  1033. c: ref('hi')
  1034. },
  1035. d: reactive({
  1036. e: ref('hello' as GT)
  1037. })
  1038. }
  1039. },
  1040. render() {
  1041. // assert setup context unwrapping
  1042. expectType<number>(this.a)
  1043. expectType<string>(this.b.c.value)
  1044. expectType<GT>(this.d.e)
  1045. // setup context properties should be mutable
  1046. this.a = 2
  1047. }
  1048. })
  1049. const vm = {} as InstanceType<typeof Comp>
  1050. // assert setup context unwrapping
  1051. expectType<number>(vm.a)
  1052. expectType<string>(vm.b.c.value)
  1053. expectType<GT>(vm.d.e)
  1054. // setup context properties should be mutable
  1055. vm.a = 2
  1056. })
  1057. // #5948
  1058. describe('DefineComponent should infer correct types when assigning to Component', () => {
  1059. let component: Component
  1060. component = defineComponent({
  1061. setup(_, { attrs, slots }) {
  1062. // @ts-expect-error should not be any
  1063. expectType<[]>(attrs)
  1064. // @ts-expect-error should not be any
  1065. expectType<[]>(slots)
  1066. }
  1067. })
  1068. expectType<Component>(component)
  1069. })
  1070. // check if defineComponent can be exported
  1071. export default {
  1072. // function components
  1073. a: defineComponent(_ => h('div')),
  1074. // no props
  1075. b: defineComponent({
  1076. data() {
  1077. return {}
  1078. }
  1079. }),
  1080. c: defineComponent({
  1081. props: ['a']
  1082. }),
  1083. d: defineComponent({
  1084. props: {
  1085. a: Number
  1086. }
  1087. })
  1088. }