define-component-test.tsx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. }
  1004. describe('functional w/ array props', () => {
  1005. const Foo = defineComponent({
  1006. functional: true,
  1007. props: ['foo'],
  1008. render(h, ctx) {
  1009. ctx.props.foo
  1010. // @ts-expect-error
  1011. ctx.props.bar
  1012. }
  1013. })
  1014. ;<Foo foo="hi" />
  1015. // @ts-expect-error
  1016. ;<Foo bar={123} />
  1017. })
  1018. describe('functional w/ object props', () => {
  1019. const Foo = defineComponent({
  1020. functional: true,
  1021. props: {
  1022. foo: String
  1023. },
  1024. render(h, ctx) {
  1025. ctx.props.foo
  1026. // @ts-expect-error
  1027. ctx.props.bar
  1028. }
  1029. })
  1030. ;<Foo foo="hi" />
  1031. // @ts-expect-error
  1032. ;<Foo foo={123} />
  1033. // @ts-expect-error
  1034. ;<Foo bar={123} />
  1035. })
  1036. // #12628
  1037. defineComponent({
  1038. components: {
  1039. App: defineComponent({})
  1040. },
  1041. data() {
  1042. return {}
  1043. },
  1044. provide(): any {
  1045. return {
  1046. fetchData: this.fetchData
  1047. }
  1048. },
  1049. created() {
  1050. this.fetchData()
  1051. },
  1052. methods: {
  1053. fetchData() {
  1054. throw new Error('Not implemented.')
  1055. }
  1056. }
  1057. })
  1058. const X = defineComponent({
  1059. methods: {
  1060. foo() {
  1061. return 123
  1062. }
  1063. }
  1064. })
  1065. // Missing / mismatching Vue 2 properties
  1066. // https://github.com/vuejs/vue/issues/12628#issuecomment-1177258223
  1067. defineComponent({
  1068. render(h) {
  1069. // vue 2
  1070. this.$listeners
  1071. this.$on('foo', () => {})
  1072. this.$ssrContext
  1073. this.$isServer
  1074. this.$children[0].$root.$children
  1075. // type casting refs
  1076. const foo = this.$refs.foo as InstanceType<typeof X>
  1077. foo.foo().toExponential()
  1078. return h('div', {}, [...this.$slots.default!])
  1079. }
  1080. })
  1081. describe('constructor attach custom properties', () => {
  1082. // #12742 allow attaching custom properties (consistent with v3)
  1083. const Foo = defineComponent({})
  1084. Foo.foobar = 123
  1085. })
  1086. describe('constructor instance type', () => {
  1087. const Comp = defineComponent({
  1088. data() {
  1089. return {
  1090. a: 1
  1091. }
  1092. },
  1093. computed: {
  1094. ac() {
  1095. return 1
  1096. }
  1097. },
  1098. methods: {
  1099. callA(b: number) {
  1100. return b
  1101. }
  1102. },
  1103. setup() {
  1104. return {
  1105. sa: '1'
  1106. }
  1107. }
  1108. })
  1109. const comp = new Comp()
  1110. expectType<number>(comp.a)
  1111. expectType<number>(comp.ac)
  1112. expectType<string>(comp.sa)
  1113. expectType<(b: number) => number>(comp.callA)
  1114. })
  1115. describe('should report non-existent properties in instance', () => {
  1116. const Foo = defineComponent({})
  1117. const instance = new Foo()
  1118. // @ts-expect-error
  1119. instance.foo
  1120. const Foo2 = defineComponent({
  1121. data() {
  1122. return {}
  1123. },
  1124. methods: {
  1125. example() {}
  1126. }
  1127. })
  1128. const instance2 = new Foo2()
  1129. // @ts-expect-error
  1130. instance2.foo
  1131. })