component.test-d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import {
  2. describe,
  3. Component,
  4. defineComponent,
  5. PropType,
  6. ref,
  7. Ref,
  8. expectError,
  9. expectType,
  10. ShallowUnwrapRef,
  11. FunctionalComponent,
  12. ComponentPublicInstance,
  13. toRefs
  14. } from './index'
  15. declare function extractComponentOptions<Props, RawBindings>(
  16. obj: Component<Props, RawBindings>
  17. ): {
  18. props: Props
  19. rawBindings: RawBindings
  20. setup: ShallowUnwrapRef<RawBindings>
  21. }
  22. describe('object props', () => {
  23. interface ExpectedProps {
  24. a?: number | undefined
  25. b: string
  26. e?: Function
  27. bb: string
  28. bbb: string
  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. validated?: string
  41. object?: object
  42. }
  43. interface ExpectedRefs {
  44. a: Ref<number | undefined>
  45. b: Ref<string>
  46. e: Ref<Function | undefined>
  47. bb: Ref<string>
  48. bbb: Ref<string>
  49. cc: Ref<string[] | undefined>
  50. dd: Ref<{ n: 1 }>
  51. ee: Ref<(() => string) | undefined>
  52. ff: Ref<((a: number, b: string) => { a: boolean }) | undefined>
  53. ccc: Ref<string[] | undefined>
  54. ddd: Ref<string[]>
  55. eee: Ref<() => { a: string }>
  56. fff: Ref<(a: number, b: string) => { a: boolean }>
  57. hhh: Ref<boolean>
  58. ggg: Ref<'foo' | 'bar'>
  59. ffff: Ref<(a: number, b: string) => { a: boolean }>
  60. validated: Ref<string | undefined>
  61. object: Ref<object | undefined>
  62. }
  63. describe('defineComponent', () => {
  64. const MyComponent = defineComponent({
  65. props: {
  66. a: Number,
  67. // required should make property non-void
  68. b: {
  69. type: String,
  70. required: true
  71. },
  72. e: Function,
  73. // default value should infer type and make it non-void
  74. bb: {
  75. default: 'hello'
  76. },
  77. bbb: {
  78. // Note: default function value requires arrow syntax + explicit
  79. // annotation
  80. default: (props: any) => (props.bb as string) || 'foo'
  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 + contructor 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: true })
  123. },
  124. validated: {
  125. type: String,
  126. // validator requires explicit annotation
  127. validator: (val: unknown) => val !== ''
  128. },
  129. object: Object as PropType<object>
  130. },
  131. setup(props) {
  132. const refs = toRefs(props)
  133. expectType<ExpectedRefs['a']>(refs.a)
  134. expectType<ExpectedRefs['b']>(refs.b)
  135. expectType<ExpectedRefs['e']>(refs.e)
  136. expectType<ExpectedRefs['bb']>(refs.bb)
  137. expectType<ExpectedRefs['bbb']>(refs.bbb)
  138. expectType<ExpectedRefs['cc']>(refs.cc)
  139. expectType<ExpectedRefs['dd']>(refs.dd)
  140. expectType<ExpectedRefs['ee']>(refs.ee)
  141. expectType<ExpectedRefs['ff']>(refs.ff)
  142. expectType<ExpectedRefs['ccc']>(refs.ccc)
  143. expectType<ExpectedRefs['ddd']>(refs.ddd)
  144. expectType<ExpectedRefs['eee']>(refs.eee)
  145. expectType<ExpectedRefs['fff']>(refs.fff)
  146. expectType<ExpectedRefs['hhh']>(refs.hhh)
  147. expectType<ExpectedRefs['ggg']>(refs.ggg)
  148. expectType<ExpectedRefs['ffff']>(refs.ffff)
  149. expectType<ExpectedRefs['validated']>(refs.validated)
  150. expectType<ExpectedRefs['object']>(refs.object)
  151. return {
  152. setupA: 1,
  153. setupB: ref(1),
  154. setupC: {
  155. a: ref(2)
  156. },
  157. setupD: undefined as Ref<number> | undefined,
  158. setupProps: props
  159. }
  160. }
  161. })
  162. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  163. // props
  164. expectType<ExpectedProps['a']>(props.a)
  165. expectType<ExpectedProps['b']>(props.b)
  166. expectType<ExpectedProps['e']>(props.e)
  167. expectType<ExpectedProps['bb']>(props.bb)
  168. expectType<ExpectedProps['bbb']>(props.bbb)
  169. expectType<ExpectedProps['cc']>(props.cc)
  170. expectType<ExpectedProps['dd']>(props.dd)
  171. expectType<ExpectedProps['ee']>(props.ee)
  172. expectType<ExpectedProps['ff']>(props.ff)
  173. expectType<ExpectedProps['ccc']>(props.ccc)
  174. expectType<ExpectedProps['ddd']>(props.ddd)
  175. expectType<ExpectedProps['eee']>(props.eee)
  176. expectType<ExpectedProps['fff']>(props.fff)
  177. expectType<ExpectedProps['hhh']>(props.hhh)
  178. expectType<ExpectedProps['ggg']>(props.ggg)
  179. expectType<ExpectedProps['ffff']>(props.ffff)
  180. expectType<ExpectedProps['validated']>(props.validated)
  181. expectType<ExpectedProps['object']>(props.object)
  182. // raw bindings
  183. expectType<Number>(rawBindings.setupA)
  184. expectType<Ref<Number>>(rawBindings.setupB)
  185. expectType<Ref<Number>>(rawBindings.setupC.a)
  186. expectType<Ref<Number> | undefined>(rawBindings.setupD)
  187. // raw bindings props
  188. expectType<ExpectedProps['a']>(rawBindings.setupProps.a)
  189. expectType<ExpectedProps['b']>(rawBindings.setupProps.b)
  190. expectType<ExpectedProps['e']>(rawBindings.setupProps.e)
  191. expectType<ExpectedProps['bb']>(rawBindings.setupProps.bb)
  192. expectType<ExpectedProps['bbb']>(rawBindings.setupProps.bbb)
  193. expectType<ExpectedProps['cc']>(rawBindings.setupProps.cc)
  194. expectType<ExpectedProps['dd']>(rawBindings.setupProps.dd)
  195. expectType<ExpectedProps['ee']>(rawBindings.setupProps.ee)
  196. expectType<ExpectedProps['ff']>(rawBindings.setupProps.ff)
  197. expectType<ExpectedProps['ccc']>(rawBindings.setupProps.ccc)
  198. expectType<ExpectedProps['ddd']>(rawBindings.setupProps.ddd)
  199. expectType<ExpectedProps['eee']>(rawBindings.setupProps.eee)
  200. expectType<ExpectedProps['fff']>(rawBindings.setupProps.fff)
  201. expectType<ExpectedProps['hhh']>(rawBindings.setupProps.hhh)
  202. expectType<ExpectedProps['ggg']>(rawBindings.setupProps.ggg)
  203. expectType<ExpectedProps['ffff']>(rawBindings.setupProps.ffff)
  204. expectType<ExpectedProps['validated']>(rawBindings.setupProps.validated)
  205. // setup
  206. expectType<Number>(setup.setupA)
  207. expectType<Number>(setup.setupB)
  208. expectType<Ref<Number>>(setup.setupC.a)
  209. expectType<number | undefined>(setup.setupD)
  210. // raw bindings props
  211. expectType<ExpectedProps['a']>(setup.setupProps.a)
  212. expectType<ExpectedProps['b']>(setup.setupProps.b)
  213. expectType<ExpectedProps['e']>(setup.setupProps.e)
  214. expectType<ExpectedProps['bb']>(setup.setupProps.bb)
  215. expectType<ExpectedProps['bbb']>(setup.setupProps.bbb)
  216. expectType<ExpectedProps['cc']>(setup.setupProps.cc)
  217. expectType<ExpectedProps['dd']>(setup.setupProps.dd)
  218. expectType<ExpectedProps['ee']>(setup.setupProps.ee)
  219. expectType<ExpectedProps['ff']>(setup.setupProps.ff)
  220. expectType<ExpectedProps['ccc']>(setup.setupProps.ccc)
  221. expectType<ExpectedProps['ddd']>(setup.setupProps.ddd)
  222. expectType<ExpectedProps['eee']>(setup.setupProps.eee)
  223. expectType<ExpectedProps['fff']>(setup.setupProps.fff)
  224. expectType<ExpectedProps['hhh']>(setup.setupProps.hhh)
  225. expectType<ExpectedProps['ggg']>(setup.setupProps.ggg)
  226. expectType<ExpectedProps['ffff']>(setup.setupProps.ffff)
  227. expectType<ExpectedProps['validated']>(setup.setupProps.validated)
  228. // instance
  229. const instance = new MyComponent()
  230. expectType<number>(instance.setupA)
  231. expectType<number | undefined>(instance.setupD)
  232. // @ts-expect-error
  233. instance.notExist
  234. })
  235. describe('options', () => {
  236. const MyComponent = {
  237. props: {
  238. a: Number,
  239. // required should make property non-void
  240. b: {
  241. type: String,
  242. required: true
  243. },
  244. e: Function,
  245. // default value should infer type and make it non-void
  246. bb: {
  247. default: 'hello'
  248. },
  249. bbb: {
  250. // Note: default function value requires arrow syntax + explicit
  251. // annotation
  252. default: (props: any) => (props.bb as string) || 'foo'
  253. },
  254. // explicit type casting
  255. cc: Array as PropType<string[]>,
  256. // required + type casting
  257. dd: {
  258. type: Object as PropType<{ n: 1 }>,
  259. required: true
  260. },
  261. // return type
  262. ee: Function as PropType<() => string>,
  263. // arguments + object return
  264. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  265. // explicit type casting with constructor
  266. ccc: Array as () => string[],
  267. // required + contructor type casting
  268. ddd: {
  269. type: Array as () => string[],
  270. required: true
  271. },
  272. // required + object return
  273. eee: {
  274. type: Function as PropType<() => { a: string }>,
  275. required: true
  276. },
  277. // required + arguments + object return
  278. fff: {
  279. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  280. required: true
  281. },
  282. hhh: {
  283. type: Boolean,
  284. required: true
  285. },
  286. // default + type casting
  287. ggg: {
  288. type: String as PropType<'foo' | 'bar'>,
  289. default: 'foo'
  290. },
  291. // default + function
  292. ffff: {
  293. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  294. default: (_a: number, _b: string) => ({ a: true })
  295. },
  296. validated: {
  297. type: String,
  298. // validator requires explicit annotation
  299. validator: (val: unknown) => val !== ''
  300. },
  301. object: Object as PropType<object>
  302. },
  303. setup() {
  304. return {
  305. setupA: 1
  306. }
  307. }
  308. } as const
  309. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  310. // props
  311. expectType<ExpectedProps['a']>(props.a)
  312. expectType<ExpectedProps['b']>(props.b)
  313. expectType<ExpectedProps['e']>(props.e)
  314. expectType<ExpectedProps['bb']>(props.bb)
  315. expectType<ExpectedProps['bbb']>(props.bbb)
  316. expectType<ExpectedProps['cc']>(props.cc)
  317. expectType<ExpectedProps['dd']>(props.dd)
  318. expectType<ExpectedProps['ee']>(props.ee)
  319. expectType<ExpectedProps['ff']>(props.ff)
  320. expectType<ExpectedProps['ccc']>(props.ccc)
  321. expectType<ExpectedProps['ddd']>(props.ddd)
  322. expectType<ExpectedProps['eee']>(props.eee)
  323. expectType<ExpectedProps['fff']>(props.fff)
  324. expectType<ExpectedProps['hhh']>(props.hhh)
  325. expectType<ExpectedProps['ggg']>(props.ggg)
  326. // expectType<ExpectedProps['ffff']>(props.ffff) // todo fix
  327. expectType<ExpectedProps['validated']>(props.validated)
  328. expectType<ExpectedProps['object']>(props.object)
  329. // rawBindings
  330. expectType<Number>(rawBindings.setupA)
  331. //setup
  332. expectType<Number>(setup.setupA)
  333. })
  334. })
  335. describe('array props', () => {
  336. describe('defineComponent', () => {
  337. const MyComponent = defineComponent({
  338. props: ['a', 'b'],
  339. setup() {
  340. return {
  341. c: 1
  342. }
  343. }
  344. })
  345. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  346. // @ts-expect-error props should be readonly
  347. expectError((props.a = 1))
  348. expectType<any>(props.a)
  349. expectType<any>(props.b)
  350. expectType<number>(rawBindings.c)
  351. expectType<number>(setup.c)
  352. })
  353. describe('options', () => {
  354. const MyComponent = {
  355. props: ['a', 'b'] as const,
  356. setup() {
  357. return {
  358. c: 1
  359. }
  360. }
  361. }
  362. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  363. // @ts-expect-error props should be readonly
  364. expectError((props.a = 1))
  365. // TODO infer the correct keys
  366. // expectType<any>(props.a)
  367. // expectType<any>(props.b)
  368. expectType<number>(rawBindings.c)
  369. expectType<number>(setup.c)
  370. })
  371. })
  372. describe('no props', () => {
  373. describe('defineComponent', () => {
  374. const MyComponent = defineComponent({
  375. setup() {
  376. return {
  377. setupA: 1
  378. }
  379. }
  380. })
  381. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  382. expectType<number>(rawBindings.setupA)
  383. expectType<number>(setup.setupA)
  384. // instance
  385. const instance = new MyComponent()
  386. expectType<number>(instance.setupA)
  387. // @ts-expect-error
  388. instance.notExist
  389. })
  390. describe('options', () => {
  391. const MyComponent = {
  392. setup() {
  393. return {
  394. setupA: 1
  395. }
  396. }
  397. }
  398. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  399. expectType<number>(rawBindings.setupA)
  400. expectType<number>(setup.setupA)
  401. })
  402. })
  403. describe('functional', () => {
  404. // TODO `props.foo` is `number|undefined`
  405. // describe('defineComponent', () => {
  406. // const MyComponent = defineComponent((props: { foo: number }) => {})
  407. // const { props } = extractComponentOptions(MyComponent)
  408. // expectType<number>(props.foo)
  409. // })
  410. describe('function', () => {
  411. const MyComponent = (props: { foo: number }) => props.foo
  412. const { props } = extractComponentOptions(MyComponent)
  413. expectType<number>(props.foo)
  414. })
  415. describe('typed', () => {
  416. const MyComponent: FunctionalComponent<{ foo: number }> = (_, _2) => {}
  417. const { props } = extractComponentOptions(MyComponent)
  418. expectType<number>(props.foo)
  419. })
  420. })
  421. declare type VueClass<Props = {}> = {
  422. new (): ComponentPublicInstance<Props>
  423. }
  424. describe('class', () => {
  425. const MyComponent: VueClass<{ foo: number }> = {} as any
  426. const { props } = extractComponentOptions(MyComponent)
  427. expectType<number>(props.foo)
  428. })