2
0

component.test-d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. } from './index'
  14. declare function extractComponentOptions<Props, RawBindings>(
  15. obj: Component<Props, RawBindings>
  16. ): {
  17. props: Props
  18. rawBindings: RawBindings
  19. setup: ShallowUnwrapRef<RawBindings>
  20. }
  21. describe('object props', () => {
  22. interface ExpectedProps {
  23. a?: number | undefined
  24. b: string
  25. e?: Function
  26. bb: string
  27. bbb: string
  28. cc?: string[] | undefined
  29. dd: { n: 1 }
  30. ee?: () => string
  31. ff?: (a: number, b: string) => { a: boolean }
  32. ccc?: string[] | undefined
  33. ddd: string[]
  34. eee: () => { a: string }
  35. fff: (a: number, b: string) => { a: boolean }
  36. hhh: boolean
  37. ggg: 'foo' | 'bar'
  38. ffff: (a: number, b: string) => { a: boolean }
  39. validated?: string
  40. }
  41. describe('defineComponent', () => {
  42. const MyComponent = defineComponent({
  43. props: {
  44. a: Number,
  45. // required should make property non-void
  46. b: {
  47. type: String,
  48. required: true
  49. },
  50. e: Function,
  51. // default value should infer type and make it non-void
  52. bb: {
  53. default: 'hello'
  54. },
  55. bbb: {
  56. // Note: default function value requires arrow syntax + explicit
  57. // annotation
  58. default: (props: any) => (props.bb as string) || 'foo'
  59. },
  60. // explicit type casting
  61. cc: Array as PropType<string[]>,
  62. // required + type casting
  63. dd: {
  64. type: Object as PropType<{ n: 1 }>,
  65. required: true
  66. },
  67. // return type
  68. ee: Function as PropType<() => string>,
  69. // arguments + object return
  70. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  71. // explicit type casting with constructor
  72. ccc: Array as () => string[],
  73. // required + contructor type casting
  74. ddd: {
  75. type: Array as () => string[],
  76. required: true
  77. },
  78. // required + object return
  79. eee: {
  80. type: Function as PropType<() => { a: string }>,
  81. required: true
  82. },
  83. // required + arguments + object return
  84. fff: {
  85. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  86. required: true
  87. },
  88. hhh: {
  89. type: Boolean,
  90. required: true
  91. },
  92. // default + type casting
  93. ggg: {
  94. type: String as PropType<'foo' | 'bar'>,
  95. default: 'foo'
  96. },
  97. // default + function
  98. ffff: {
  99. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  100. default: (_a: number, _b: string) => ({ a: true })
  101. },
  102. validated: {
  103. type: String,
  104. // validator requires explicit annotation
  105. validator: (val: unknown) => val !== ''
  106. }
  107. },
  108. setup(props) {
  109. return {
  110. setupA: 1,
  111. setupB: ref(1),
  112. setupC: {
  113. a: ref(2)
  114. },
  115. setupProps: props
  116. }
  117. }
  118. })
  119. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  120. // props
  121. expectType<ExpectedProps['a']>(props.a)
  122. expectType<ExpectedProps['b']>(props.b)
  123. expectType<ExpectedProps['e']>(props.e)
  124. expectType<ExpectedProps['bb']>(props.bb)
  125. expectType<ExpectedProps['bbb']>(props.bbb)
  126. expectType<ExpectedProps['cc']>(props.cc)
  127. expectType<ExpectedProps['dd']>(props.dd)
  128. expectType<ExpectedProps['ee']>(props.ee)
  129. expectType<ExpectedProps['ff']>(props.ff)
  130. expectType<ExpectedProps['ccc']>(props.ccc)
  131. expectType<ExpectedProps['ddd']>(props.ddd)
  132. expectType<ExpectedProps['eee']>(props.eee)
  133. expectType<ExpectedProps['fff']>(props.fff)
  134. expectType<ExpectedProps['hhh']>(props.hhh)
  135. expectType<ExpectedProps['ggg']>(props.ggg)
  136. expectType<ExpectedProps['ffff']>(props.ffff)
  137. expectType<ExpectedProps['validated']>(props.validated)
  138. // raw bindings
  139. expectType<Number>(rawBindings.setupA)
  140. expectType<Ref<Number>>(rawBindings.setupB)
  141. expectType<Ref<Number>>(rawBindings.setupC.a)
  142. expectType<Number>(rawBindings.setupA)
  143. // raw bindings props
  144. expectType<ExpectedProps['a']>(rawBindings.setupProps.a)
  145. expectType<ExpectedProps['b']>(rawBindings.setupProps.b)
  146. expectType<ExpectedProps['e']>(rawBindings.setupProps.e)
  147. expectType<ExpectedProps['bb']>(rawBindings.setupProps.bb)
  148. expectType<ExpectedProps['bbb']>(rawBindings.setupProps.bbb)
  149. expectType<ExpectedProps['cc']>(rawBindings.setupProps.cc)
  150. expectType<ExpectedProps['dd']>(rawBindings.setupProps.dd)
  151. expectType<ExpectedProps['ee']>(rawBindings.setupProps.ee)
  152. expectType<ExpectedProps['ff']>(rawBindings.setupProps.ff)
  153. expectType<ExpectedProps['ccc']>(rawBindings.setupProps.ccc)
  154. expectType<ExpectedProps['ddd']>(rawBindings.setupProps.ddd)
  155. expectType<ExpectedProps['eee']>(rawBindings.setupProps.eee)
  156. expectType<ExpectedProps['fff']>(rawBindings.setupProps.fff)
  157. expectType<ExpectedProps['hhh']>(rawBindings.setupProps.hhh)
  158. expectType<ExpectedProps['ggg']>(rawBindings.setupProps.ggg)
  159. expectType<ExpectedProps['ffff']>(rawBindings.setupProps.ffff)
  160. expectType<ExpectedProps['validated']>(rawBindings.setupProps.validated)
  161. // setup
  162. expectType<Number>(setup.setupA)
  163. expectType<Number>(setup.setupB)
  164. expectType<Ref<Number>>(setup.setupC.a)
  165. expectType<Number>(setup.setupA)
  166. // raw bindings props
  167. expectType<ExpectedProps['a']>(setup.setupProps.a)
  168. expectType<ExpectedProps['b']>(setup.setupProps.b)
  169. expectType<ExpectedProps['e']>(setup.setupProps.e)
  170. expectType<ExpectedProps['bb']>(setup.setupProps.bb)
  171. expectType<ExpectedProps['bbb']>(setup.setupProps.bbb)
  172. expectType<ExpectedProps['cc']>(setup.setupProps.cc)
  173. expectType<ExpectedProps['dd']>(setup.setupProps.dd)
  174. expectType<ExpectedProps['ee']>(setup.setupProps.ee)
  175. expectType<ExpectedProps['ff']>(setup.setupProps.ff)
  176. expectType<ExpectedProps['ccc']>(setup.setupProps.ccc)
  177. expectType<ExpectedProps['ddd']>(setup.setupProps.ddd)
  178. expectType<ExpectedProps['eee']>(setup.setupProps.eee)
  179. expectType<ExpectedProps['fff']>(setup.setupProps.fff)
  180. expectType<ExpectedProps['hhh']>(setup.setupProps.hhh)
  181. expectType<ExpectedProps['ggg']>(setup.setupProps.ggg)
  182. expectType<ExpectedProps['ffff']>(setup.setupProps.ffff)
  183. expectType<ExpectedProps['validated']>(setup.setupProps.validated)
  184. })
  185. describe('options', () => {
  186. const MyComponent = {
  187. props: {
  188. a: Number,
  189. // required should make property non-void
  190. b: {
  191. type: String,
  192. required: true
  193. },
  194. e: Function,
  195. // default value should infer type and make it non-void
  196. bb: {
  197. default: 'hello'
  198. },
  199. bbb: {
  200. // Note: default function value requires arrow syntax + explicit
  201. // annotation
  202. default: (props: any) => (props.bb as string) || 'foo'
  203. },
  204. // explicit type casting
  205. cc: Array as PropType<string[]>,
  206. // required + type casting
  207. dd: {
  208. type: Object as PropType<{ n: 1 }>,
  209. required: true
  210. },
  211. // return type
  212. ee: Function as PropType<() => string>,
  213. // arguments + object return
  214. ff: Function as PropType<(a: number, b: string) => { a: boolean }>,
  215. // explicit type casting with constructor
  216. ccc: Array as () => string[],
  217. // required + contructor type casting
  218. ddd: {
  219. type: Array as () => string[],
  220. required: true
  221. },
  222. // required + object return
  223. eee: {
  224. type: Function as PropType<() => { a: string }>,
  225. required: true
  226. },
  227. // required + arguments + object return
  228. fff: {
  229. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  230. required: true
  231. },
  232. hhh: {
  233. type: Boolean,
  234. required: true
  235. },
  236. // default + type casting
  237. ggg: {
  238. type: String as PropType<'foo' | 'bar'>,
  239. default: 'foo'
  240. },
  241. // default + function
  242. ffff: {
  243. type: Function as PropType<(a: number, b: string) => { a: boolean }>,
  244. default: (_a: number, _b: string) => ({ a: true })
  245. },
  246. validated: {
  247. type: String,
  248. // validator requires explicit annotation
  249. validator: (val: unknown) => val !== ''
  250. }
  251. },
  252. setup() {
  253. return {
  254. setupA: 1
  255. }
  256. }
  257. } as const
  258. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  259. // props
  260. expectType<ExpectedProps['a']>(props.a)
  261. expectType<ExpectedProps['b']>(props.b)
  262. expectType<ExpectedProps['e']>(props.e)
  263. expectType<ExpectedProps['bb']>(props.bb)
  264. expectType<ExpectedProps['bbb']>(props.bbb)
  265. expectType<ExpectedProps['cc']>(props.cc)
  266. expectType<ExpectedProps['dd']>(props.dd)
  267. expectType<ExpectedProps['ee']>(props.ee)
  268. expectType<ExpectedProps['ff']>(props.ff)
  269. expectType<ExpectedProps['ccc']>(props.ccc)
  270. expectType<ExpectedProps['ddd']>(props.ddd)
  271. expectType<ExpectedProps['eee']>(props.eee)
  272. expectType<ExpectedProps['fff']>(props.fff)
  273. expectType<ExpectedProps['hhh']>(props.hhh)
  274. expectType<ExpectedProps['ggg']>(props.ggg)
  275. // expectType<ExpectedProps['ffff']>(props.ffff) // todo fix
  276. expectType<ExpectedProps['validated']>(props.validated)
  277. // rawBindings
  278. expectType<Number>(rawBindings.setupA)
  279. //setup
  280. expectType<Number>(setup.setupA)
  281. })
  282. })
  283. describe('array props', () => {
  284. describe('defineComponent', () => {
  285. const MyComponent = defineComponent({
  286. props: ['a', 'b'],
  287. setup() {
  288. return {
  289. c: 1
  290. }
  291. }
  292. })
  293. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  294. // @ts-expect-error props should be readonly
  295. expectError((props.a = 1))
  296. expectType<any>(props.a)
  297. expectType<any>(props.b)
  298. expectType<number>(rawBindings.c)
  299. expectType<number>(setup.c)
  300. })
  301. describe('options', () => {
  302. const MyComponent = {
  303. props: ['a', 'b'] as const,
  304. setup() {
  305. return {
  306. c: 1
  307. }
  308. }
  309. }
  310. const { props, rawBindings, setup } = extractComponentOptions(MyComponent)
  311. // @ts-expect-error props should be readonly
  312. expectError((props.a = 1))
  313. // TODO infer the correct keys
  314. // expectType<any>(props.a)
  315. // expectType<any>(props.b)
  316. expectType<number>(rawBindings.c)
  317. expectType<number>(setup.c)
  318. })
  319. })
  320. describe('no props', () => {
  321. describe('defineComponent', () => {
  322. const MyComponent = defineComponent({
  323. setup() {
  324. return {
  325. setupA: 1
  326. }
  327. }
  328. })
  329. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  330. expectType<number>(rawBindings.setupA)
  331. expectType<number>(setup.setupA)
  332. })
  333. describe('options', () => {
  334. const MyComponent = {
  335. setup() {
  336. return {
  337. setupA: 1
  338. }
  339. }
  340. }
  341. const { rawBindings, setup } = extractComponentOptions(MyComponent)
  342. expectType<number>(rawBindings.setupA)
  343. expectType<number>(setup.setupA)
  344. })
  345. })
  346. describe('functional', () => {
  347. // TODO `props.foo` is `number|undefined`
  348. // describe('defineComponent', () => {
  349. // const MyComponent = defineComponent((props: { foo: number }) => {})
  350. // const { props } = extractComponentOptions(MyComponent)
  351. // expectType<number>(props.foo)
  352. // })
  353. describe('function', () => {
  354. const MyComponent = (props: { foo: number }) => props.foo
  355. const { props } = extractComponentOptions(MyComponent)
  356. expectType<number>(props.foo)
  357. })
  358. describe('typed', () => {
  359. const MyComponent: FunctionalComponent<{ foo: number }> = (_, _2) => {}
  360. const { props } = extractComponentOptions(MyComponent)
  361. expectType<number>(props.foo)
  362. })
  363. })
  364. declare type VueClass<Props = {}> = {
  365. new (): ComponentPublicInstance<Props>
  366. }
  367. describe('class', () => {
  368. const MyComponent: VueClass<{ foo: number }> = {} as any
  369. const { props } = extractComponentOptions(MyComponent)
  370. expectType<number>(props.foo)
  371. })