directives.test-d.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = any,
  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: Partial<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: Partial<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: Partial<Record<'a' | 'b', boolean>>
  49. // @ts-expect-error
  50. }>(testDirective<string, 'a' | 'b', 'Arg'>())
  51. expectType<{
  52. value: number
  53. oldValue: number | null
  54. arg?: HTMLElement
  55. modifiers: Partial<Record<'a' | 'b', boolean>>
  56. }>(testDirective<number, 'a' | 'b', HTMLElement>())
  57. expectType<{
  58. value: number
  59. oldValue: number | null
  60. arg?: HTMLElement
  61. modifiers: Partial<Record<'a' | 'b', boolean>>
  62. // @ts-expect-error
  63. }>(testDirective<number, 'a' | 'b', string>())
  64. expectType<{
  65. value: number
  66. oldValue: number | null
  67. arg?: HTMLElement
  68. modifiers: Partial<Record<'a' | 'b', boolean>>
  69. }>(testDirective<number, 'a' | 'b'>())
  70. })