directives.test-d.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { type Directive, type ObjectDirective, vModelText } from 'vue'
  2. import { describe, expectType } from './utils'
  3. type ExtractBinding<T> = T extends (
  4. el: any,
  5. binding: infer B,
  6. vnode: any,
  7. prev: any,
  8. ) => any
  9. ? B
  10. : never
  11. declare function testDirective<
  12. Value,
  13. Modifiers extends string = string,
  14. Arg extends string = string,
  15. >(): ExtractBinding<Directive<any, Value, Modifiers, Arg>>
  16. describe('vmodel', () => {
  17. expectType<ObjectDirective<any, any, 'trim' | 'number' | 'lazy', string>>(
  18. vModelText,
  19. )
  20. // @ts-expect-error
  21. expectType<ObjectDirective<any, any, 'not-valid', string>>(vModelText)
  22. })
  23. describe('custom', () => {
  24. expectType<{
  25. value: number
  26. oldValue: number | null
  27. arg?: 'Arg'
  28. modifiers: Record<'a' | 'b', boolean>
  29. }>(testDirective<number, 'a' | 'b', 'Arg'>())
  30. expectType<{
  31. value: number
  32. oldValue: number | null
  33. arg?: 'Arg'
  34. modifiers: Record<'a' | 'b', boolean>
  35. // @ts-expect-error
  36. }>(testDirective<number, 'a', 'Arg'>())
  37. expectType<{
  38. value: number
  39. oldValue: number | null
  40. arg?: 'Arg'
  41. modifiers: Record<'a' | 'b', boolean>
  42. // @ts-expect-error
  43. }>(testDirective<number, 'a' | 'b', 'Argx'>())
  44. expectType<{
  45. value: number
  46. oldValue: number | null
  47. arg?: 'Arg'
  48. modifiers: Record<'a' | 'b', boolean>
  49. // @ts-expect-error
  50. }>(testDirective<string, 'a' | 'b', 'Arg'>())
  51. })