2
0

defineComponent.test-d.tsx 29 KB

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