defineComponent.test-d.tsx 28 KB

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