component.test-d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. setupProps: props
  158. }
  159. }
  160. })
  161. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  162. // props
  163. expectType<ExpectedProps['a']>(props.a)
  164. expectType<ExpectedProps['b']>(props.b)
  165. expectType<ExpectedProps['e']>(props.e)
  166. expectType<ExpectedProps['bb']>(props.bb)
  167. expectType<ExpectedProps['bbb']>(props.bbb)
  168. expectType<ExpectedProps['cc']>(props.cc)
  169. expectType<ExpectedProps['dd']>(props.dd)
  170. expectType<ExpectedProps['ee']>(props.ee)
  171. expectType<ExpectedProps['ff']>(props.ff)
  172. expectType<ExpectedProps['ccc']>(props.ccc)
  173. expectType<ExpectedProps['ddd']>(props.ddd)
  174. expectType<ExpectedProps['eee']>(props.eee)
  175. expectType<ExpectedProps['fff']>(props.fff)
  176. expectType<ExpectedProps['hhh']>(props.hhh)
  177. expectType<ExpectedProps['ggg']>(props.ggg)
  178. expectType<ExpectedProps['ffff']>(props.ffff)
  179. expectType<ExpectedProps['validated']>(props.validated)
  180. expectType<ExpectedProps['object']>(props.object)
  181. // raw bindings
  182. expectType<Number>(rawBindings.setupA)
  183. expectType<Ref<Number>>(rawBindings.setupB)
  184. expectType<Ref<Number>>(rawBindings.setupC.a)
  185. expectType<Number>(rawBindings.setupA)
  186. // raw bindings props
  187. expectType<ExpectedProps['a']>(rawBindings.setupProps.a)
  188. expectType<ExpectedProps['b']>(rawBindings.setupProps.b)
  189. expectType<ExpectedProps['e']>(rawBindings.setupProps.e)
  190. expectType<ExpectedProps['bb']>(rawBindings.setupProps.bb)
  191. expectType<ExpectedProps['bbb']>(rawBindings.setupProps.bbb)
  192. expectType<ExpectedProps['cc']>(rawBindings.setupProps.cc)
  193. expectType<ExpectedProps['dd']>(rawBindings.setupProps.dd)
  194. expectType<ExpectedProps['ee']>(rawBindings.setupProps.ee)
  195. expectType<ExpectedProps['ff']>(rawBindings.setupProps.ff)
  196. expectType<ExpectedProps['ccc']>(rawBindings.setupProps.ccc)
  197. expectType<ExpectedProps['ddd']>(rawBindings.setupProps.ddd)
  198. expectType<ExpectedProps['eee']>(rawBindings.setupProps.eee)
  199. expectType<ExpectedProps['fff']>(rawBindings.setupProps.fff)
  200. expectType<ExpectedProps['hhh']>(rawBindings.setupProps.hhh)
  201. expectType<ExpectedProps['ggg']>(rawBindings.setupProps.ggg)
  202. expectType<ExpectedProps['ffff']>(rawBindings.setupProps.ffff)
  203. expectType<ExpectedProps['validated']>(rawBindings.setupProps.validated)
  204. // setup
  205. expectType<Number>(setup.setupA)
  206. expectType<Number>(setup.setupB)
  207. expectType<Ref<Number>>(setup.setupC.a)
  208. expectType<Number>(setup.setupA)
  209. // raw bindings props
  210. expectType<ExpectedProps['a']>(setup.setupProps.a)
  211. expectType<ExpectedProps['b']>(setup.setupProps.b)
  212. expectType<ExpectedProps['e']>(setup.setupProps.e)
  213. expectType<ExpectedProps['bb']>(setup.setupProps.bb)
  214. expectType<ExpectedProps['bbb']>(setup.setupProps.bbb)
  215. expectType<ExpectedProps['cc']>(setup.setupProps.cc)
  216. expectType<ExpectedProps['dd']>(setup.setupProps.dd)
  217. expectType<ExpectedProps['ee']>(setup.setupProps.ee)
  218. expectType<ExpectedProps['ff']>(setup.setupProps.ff)
  219. expectType<ExpectedProps['ccc']>(setup.setupProps.ccc)
  220. expectType<ExpectedProps['ddd']>(setup.setupProps.ddd)
  221. expectType<ExpectedProps['eee']>(setup.setupProps.eee)
  222. expectType<ExpectedProps['fff']>(setup.setupProps.fff)
  223. expectType<ExpectedProps['hhh']>(setup.setupProps.hhh)
  224. expectType<ExpectedProps['ggg']>(setup.setupProps.ggg)
  225. expectType<ExpectedProps['ffff']>(setup.setupProps.ffff)
  226. expectType<ExpectedProps['validated']>(setup.setupProps.validated)
  227. // instance
  228. const instance = new MyComponent()
  229. expectType<number>(instance.setupA)
  230. // @ts-expect-error
  231. instance.notExist
  232. })
  233. describe('options', () => {
  234. const MyComponent = {
  235. props: {
  236. a: Number,
  237. // required should make property non-void
  238. b: {
  239. type: String,
  240. required: true
  241. },
  242. e: Function,
  243. // default value should infer type and make it non-void
  244. bb: {
  245. default: 'hello'
  246. },
  247. bbb: {
  248. // Note: default function value requires arrow syntax + explicit
  249. // annotation
  250. default: (props: any) => (props.bb as string) || 'foo'
  251. },
  252. // explicit type casting
  253. cc: Array as PropType<string[]>,
  254. // required + type casting
  255. dd: {
  256. type: Object as PropType<{ n: 1 }>,
  257. required: true
  258. },
  259. // return type
  260. ee: Function as PropType<() => string>,
  261. // arguments + object return
  262. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  263. // explicit type casting with constructor
  264. ccc: Array as () => string[],
  265. // required + contructor type casting
  266. ddd: {
  267. type: Array as () => string[],
  268. required: true
  269. },
  270. // required + object return
  271. eee: {
  272. type: Function as PropType<() => { a: string }>,
  273. required: true
  274. },
  275. // required + arguments + object return
  276. fff: {
  277. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  278. required: true
  279. },
  280. hhh: {
  281. type: Boolean,
  282. required: true
  283. },
  284. // default + type casting
  285. ggg: {
  286. type: String as PropType<'foo' | 'bar'>,
  287. default: 'foo'
  288. },
  289. // default + function
  290. ffff: {
  291. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  292. default: (_a: number, _b: string) => ({ a: true })
  293. },
  294. validated: {
  295. type: String,
  296. // validator requires explicit annotation
  297. validator: (val: unknown) => val !== ''
  298. },
  299. object: Object as PropType<object>
  300. },
  301. setup() {
  302. return {
  303. setupA: 1
  304. }
  305. }
  306. } as const
  307. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  308. // props
  309. expectType<ExpectedProps['a']>(props.a)
  310. expectType<ExpectedProps['b']>(props.b)
  311. expectType<ExpectedProps['e']>(props.e)
  312. expectType<ExpectedProps['bb']>(props.bb)
  313. expectType<ExpectedProps['bbb']>(props.bbb)
  314. expectType<ExpectedProps['cc']>(props.cc)
  315. expectType<ExpectedProps['dd']>(props.dd)
  316. expectType<ExpectedProps['ee']>(props.ee)
  317. expectType<ExpectedProps['ff']>(props.ff)
  318. expectType<ExpectedProps['ccc']>(props.ccc)
  319. expectType<ExpectedProps['ddd']>(props.ddd)
  320. expectType<ExpectedProps['eee']>(props.eee)
  321. expectType<ExpectedProps['fff']>(props.fff)
  322. expectType<ExpectedProps['hhh']>(props.hhh)
  323. expectType<ExpectedProps['ggg']>(props.ggg)
  324. // expectType<ExpectedProps['ffff']>(props.ffff) // todo fix
  325. expectType<ExpectedProps['validated']>(props.validated)
  326. expectType<ExpectedProps['object']>(props.object)
  327. // rawBindings
  328. expectType<Number>(rawBindings.setupA)
  329. //setup
  330. expectType<Number>(setup.setupA)
  331. })
  332. })
  333. describe('array props', () => {
  334. describe('defineComponent', () => {
  335. const MyComponent = defineComponent({
  336. props: ['a', 'b'],
  337. setup() {
  338. return {
  339. c: 1
  340. }
  341. }
  342. })
  343. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  344. // @ts-expect-error props should be readonly
  345. expectError((props.a = 1))
  346. expectType<any>(props.a)
  347. expectType<any>(props.b)
  348. expectType<number>(rawBindings.c)
  349. expectType<number>(setup.c)
  350. })
  351. describe('options', () => {
  352. const MyComponent = {
  353. props: ['a', 'b'] as const,
  354. setup() {
  355. return {
  356. c: 1
  357. }
  358. }
  359. }
  360. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  361. // @ts-expect-error props should be readonly
  362. expectError((props.a = 1))
  363. // TODO infer the correct keys
  364. // expectType<any>(props.a)
  365. // expectType<any>(props.b)
  366. expectType<number>(rawBindings.c)
  367. expectType<number>(setup.c)
  368. })
  369. })
  370. describe('no props', () => {
  371. describe('defineComponent', () => {
  372. const MyComponent = defineComponent({
  373. setup() {
  374. return {
  375. setupA: 1
  376. }
  377. }
  378. })
  379. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  380. expectType<number>(rawBindings.setupA)
  381. expectType<number>(setup.setupA)
  382. // instance
  383. const instance = new MyComponent()
  384. expectType<number>(instance.setupA)
  385. // @ts-expect-error
  386. instance.notExist
  387. })
  388. describe('options', () => {
  389. const MyComponent = {
  390. setup() {
  391. return {
  392. setupA: 1
  393. }
  394. }
  395. }
  396. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  397. expectType<number>(rawBindings.setupA)
  398. expectType<number>(setup.setupA)
  399. })
  400. })
  401. describe('functional', () => {
  402. // TODO `props.foo` is `number|undefined`
  403. // describe('defineComponent', () => {
  404. // const MyComponent = defineComponent((props: { foo: number }) => {})
  405. // const { props } = extractComponentOptions(MyComponent)
  406. // expectType<number>(props.foo)
  407. // })
  408. describe('function', () => {
  409. const MyComponent = (props: { foo: number }) => props.foo
  410. const { props } = extractComponentOptions(MyComponent)
  411. expectType<number>(props.foo)
  412. })
  413. describe('typed', () => {
  414. const MyComponent: FunctionalComponent<{ foo: number }> = (_, _2) => {}
  415. const { props } = extractComponentOptions(MyComponent)
  416. expectType<number>(props.foo)
  417. })
  418. })
  419. declare type VueClass<Props = {}> = {
  420. new (): ComponentPublicInstance<Props>
  421. }
  422. describe('class', () => {
  423. const MyComponent: VueClass<{ foo: number }> = {} as any
  424. const { props } = extractComponentOptions(MyComponent)
  425. expectType<number>(props.foo)
  426. })