eslint-plugin-vue icon indicating copy to clipboard operation
eslint-plugin-vue copied to clipboard

`vue/require-default-prop` should not trigger when type is `x | undefined` without default value

Open Aspyryan opened this issue 1 year ago • 6 comments

What rule do you want to change? vue/require-default-prop

Does this change cause the rule to produce more or fewer warnings? Is will provide less warnings if the type is already defined as undefined.

How will the change be implemented? (New option, new default behavior, etc.)? Could be an option to not throw the error / warning when props is not required and has a undefined type set

Please provide some example code that this change will affect:

const props = withDefaults(
  defineProps<{
    // Is not throwing warning because default is set
    modelValue?: string;
    // Should throw warning because no default is set
    label?: string;
    // Should not throw warning because undefined is allowed
    propertyName?: string | undefined;
  }>(),
  {
    modelValue: 'test',
  },
);

What does the rule currently do for this code? Throws a warning at the propertyName prop

What will the rule do after it's changed? Will not complain about the propertyName prop

Additional context If needed, I can look into it myself but I will need to be pointed to the correct files :)

Aspyryan avatar Jun 12 '24 08:06 Aspyryan

Related to

  • https://github.com/vuejs/eslint-plugin-vue/issues/2051
  • https://github.com/vuejs/eslint-plugin-vue/issues/2376

FloEdelmann avatar Jun 12 '24 08:06 FloEdelmann

I'm not sure if I agree; every optional property (with a ?) automatically has | undefined added to its type, so it would render the rule useless if every optional prop would not be required to have a default value then.

FloEdelmann avatar Jun 12 '24 08:06 FloEdelmann

Hmm yea I get that, is it typescript doing the automatic undefined? can we somehow disable that and then apply the rule?

Aspyryan avatar Jun 12 '24 09:06 Aspyryan

Yes it is. I don't think we can turn that off.

FloEdelmann avatar Jun 12 '24 11:06 FloEdelmann

https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes

I'm using it in my projects to force an explicit undefined on optional properties where expected.

Not sure if it helps with this exact situation, but there is support in TS for it.

One of our dependencies requires it: https://github.com/Effect-TS/effect/tree/main/packages/schema#requirements.

higherorderfunctor avatar Jun 17 '24 23:06 higherorderfunctor

Just updated from 10.0.0 where I had no eslint error to 10.1.0, and now I have vue/require-default-prop popping everywhere. If I add the default value, eslint will then complain the the variable are unused since they are only used in the template and not the script part.

Before: Image

After: Image

mrleblanc101 avatar May 09 '25 17:05 mrleblanc101