defineComponent.test-d.tsx 30 KB

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