core icon indicating copy to clipboard operation
core copied to clipboard

types(runtime-core): enable plugin option types

Open tony19 opened this issue 3 years ago β€’ 6 comments

This enables type inference for:

  1. The options arg of a plugin's install function
  2. The options passed to createApp().use()
/**
 * Plugin
 */
type PluginAOptionType = {
  option1?: string;
  option2: number;
  option3: boolean;
}

const PluginA = {
  install(app: App, ...options: PluginAOptionType[]) {

  }
}

const PluginB = {
  install(app: App) {

  }
}

/**
 * Main
 */
createApp({})
  .use(PluginA, { option3: true }) // ❌ option2 (required) missing
  .use(PluginA, {}) // ❌ option2 and option3 (required) missing
  .use(PluginA) // βœ…
  .use(PluginA, { option2: 1, option3: true }) // βœ…
  .use(PluginA, { option1: 'foo', option2: 1, option3: true }) // βœ…
  
  .use(PluginB) // βœ…
  .use(PluginB, {}) // ❌ unexpected plugin option

demo

tony19 avatar Jun 17 '21 04:06 tony19

Thank you but there are already a few open PRs for this: https://github.com/vuejs/vue-next/pull/2758 https://github.com/vuejs/vue-next/pull/2589

posva avatar Jun 17 '21 08:06 posva

@posva Oh, I missed those PRs. However, they appear to be outdated, as they no longer compile. Plus, I believe they'll introduce a breaking change for users. I'll comment in a moment.

tony19 avatar Jun 17 '21 22:06 tony19

@tony19 Can you help resolve the conflicts? Thanks

antfu avatar Oct 03 '22 08:10 antfu

@antfu Done. Thanks!

tony19 avatar Oct 08 '22 01:10 tony19

@posva I agree, and the current code already allows that πŸ˜„

tony19 avatar Oct 09 '22 03:10 tony19

You were right! but it also should be stricter πŸ˜„ I’m reviewing on the phone so I can’t do much code but I will give it a proper review with help later

posva avatar Oct 10 '22 12:10 posva