appUse.test-d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { type App, type Plugin, createApp, defineComponent } from 'vue'
  2. const app = createApp({})
  3. // Plugin without types accept anything
  4. const PluginWithoutType: Plugin = {
  5. install(app: App) {},
  6. }
  7. app.use(PluginWithoutType)
  8. app.use(PluginWithoutType, 2)
  9. app.use(PluginWithoutType, { anything: 'goes' }, true)
  10. type PluginOptions = {
  11. /** option1 */
  12. option1?: string
  13. /** option2 */
  14. option2: number
  15. /** option3 */
  16. option3: boolean
  17. }
  18. const PluginWithObjectOptions = {
  19. install(app: App, options: PluginOptions) {
  20. options.option1
  21. options.option2
  22. options.option3
  23. },
  24. }
  25. const objectPluginOptional = {
  26. install(app: App, options?: PluginOptions) {},
  27. }
  28. app.use(objectPluginOptional)
  29. app.use(
  30. objectPluginOptional,
  31. // Test JSDoc and `go to definition` for options
  32. {
  33. option1: 'foo',
  34. option2: 1,
  35. option3: true,
  36. },
  37. )
  38. for (const Plugin of [
  39. PluginWithObjectOptions,
  40. PluginWithObjectOptions.install,
  41. ]) {
  42. // @ts-expect-error: no params
  43. app.use(Plugin)
  44. // @ts-expect-error option2 and option3 (required) missing
  45. app.use(Plugin, {})
  46. // @ts-expect-error type mismatch
  47. app.use(Plugin, undefined)
  48. // valid options
  49. app.use(Plugin, { option2: 1, option3: true })
  50. app.use(Plugin, { option1: 'foo', option2: 1, option3: true })
  51. }
  52. const PluginNoOptions = {
  53. install(app: App) {},
  54. }
  55. for (const Plugin of [PluginNoOptions, PluginNoOptions.install]) {
  56. // no args
  57. app.use(Plugin)
  58. // @ts-expect-error unexpected plugin option
  59. app.use(Plugin, {})
  60. // @ts-expect-error only no options is valid
  61. app.use(Plugin, undefined)
  62. }
  63. const PluginMultipleArgs = {
  64. install: (app: App, a: string, b: number) => {},
  65. }
  66. for (const Plugin of [PluginMultipleArgs, PluginMultipleArgs.install]) {
  67. // @ts-expect-error: 2 arguments expected
  68. app.use(Plugin, 'hey')
  69. app.use(Plugin, 'hey', 2)
  70. }
  71. const PluginOptionalOptions = {
  72. install(
  73. app: App,
  74. options: PluginOptions = { option2: 2, option3: true, option1: 'foo' },
  75. ) {
  76. options.option1
  77. options.option2
  78. options.option3
  79. },
  80. }
  81. for (const Plugin of [PluginOptionalOptions, PluginOptionalOptions.install]) {
  82. // both version are valid
  83. app.use(Plugin)
  84. app.use(Plugin, undefined)
  85. // @ts-expect-error option2 and option3 (required) missing
  86. app.use(Plugin, {})
  87. // valid options
  88. app.use(Plugin, { option2: 1, option3: true })
  89. app.use(Plugin, { option1: 'foo', option2: 1, option3: true })
  90. }
  91. // still valid but it's better to use the regular function because this one can accept an optional param
  92. const PluginTyped: Plugin<PluginOptions> = (app, options) => {}
  93. // @ts-expect-error: needs options
  94. app.use(PluginTyped)
  95. app.use(
  96. PluginTyped,
  97. // Test autocomplete for options
  98. {
  99. option1: '',
  100. option2: 2,
  101. option3: true,
  102. },
  103. )
  104. const functionPluginOptional = (app: App, options?: PluginOptions) => {}
  105. app.use(functionPluginOptional)
  106. app.use(functionPluginOptional, { option2: 2, option3: true })
  107. // type optional params
  108. const functionPluginOptional2: Plugin<[options?: PluginOptions]> = (
  109. app,
  110. options,
  111. ) => {}
  112. app.use(functionPluginOptional2)
  113. app.use(functionPluginOptional2, { option2: 2, option3: true })
  114. // vuetify usage
  115. const key: string = ''
  116. const aliases: Record<string, any> = {}
  117. app.component(
  118. key,
  119. defineComponent({
  120. ...aliases[key],
  121. name: key,
  122. aliasName: aliases[key].name,
  123. }),
  124. )