define-component-test.tsx 27 KB

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