Skip to content

fix(ts): force mandatory options #10575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

fix(ts): force mandatory options #10575

wants to merge 4 commits into from

Conversation

bitPogo
Copy link

@bitPogo bitPogo commented Sep 27, 2019

Closes #10573

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • [ x ] Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • [ x ] No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • [ x ] A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:
Example: https://tinyurl.com/y2uzn6k2

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests to the plugin-test.ts file in the types folder:

  • One that checks that we can use a plugin that takes no options
  • Another one that checks we can use a plugin with optional options both, with a second parameter, and without

@@ -15,6 +15,9 @@ const plugin: PluginObject<Option> = {
}
const installer: PluginFunction<Option> = function(Vue, option) { }

Vue.use(plugin);
Copy link
Member

@posva posva Sep 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one should yield an error though 🤔

The tests that must be added are:

function NoOptions(_Vue: Vue): void {}
function OptionalOption(_Vue: Vue, options?: Option): void {}

Vue.use(NoOptions)
Vue.use(OptionalOption)
Vue.use(OptionalOption, { prefix: '', suffix: '' })

I'm realizing that the types you put in vue.d.ts are different from what you tried on the TS playground

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It passes also in current dev

Thanks for that advises.

Indeed is a minor difference to the mentioned playground. The reason is
Vue.use(installer, new Option, new Option, new Option);
Would fail, since the definition hits in the master the any case of use, which would not work with unknown. (I am even not sure if this was intended in the master)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know, if the any fallback was intended on Vue.use<PluginOption>. This could behavior could be restored by a additional definition use<T>(plugin: PluginObject<T> | PluginFunction<T>, option1: any, option2: any, ...options: any[] ): VueConstructor<V>;. It looks perhaps a bit odd, but use and a given type should fallback at n+1( n>1 ) arguments toany according to master.
( Please see: https://tinyurl.com/y3lcapvv )

@@ -14,7 +14,13 @@ const plugin: PluginObject<Option> = {
}
}
const installer: PluginFunction<Option> = function(Vue, option) { }
function NoOptions( _vue: typeof Vue ){};
Copy link
Member

@posva posva Sep 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typeof shouldn't be necessary, maybe we should use PluginFunction<never>, also it's better to use an uppercase version like _Vue or Vue

Copy link
Author

@bitPogo bitPogo Sep 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since is defined as PluginFunction<T> = (Vue: typeof _Vue, options?: T) => void; the compiler will complain on a direct usage of Vue as a type. I changed it to VueConstructor, so it should be clearer.

@@ -111,8 +111,9 @@ export interface VueConstructor<V extends Vue = Vue> {
component<Props>(id: string, definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
component(id: string, definition?: ComponentOptions<V>): ExtendedVue<V, {}, {}, {}, {}>;

use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): VueConstructor<V>;
use(plugin: PluginObject<any> | PluginFunction<any>, ...options: any[]): VueConstructor<V>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test

@posva
Copy link
Member

posva commented Aug 8, 2020

@pkf1994 This is the 3rd time you spam with tests. Don't do it again or we will have to block you.

@bitPogo
Copy link
Author

bitPogo commented Dec 31, 2022

Since this is now ancient...I am closing it - happy new year!

@bitPogo bitPogo closed this Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent Vue.use to be used without options parameter unless it's specified as optional (with ?)
3 participants