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

Force TypeScript definition of not primitive props

Open przemyslawjanpietrzak opened this issue 3 years ago • 5 comments

Please describe what the rule should do: Rule should throw error/warning when component has declared prop without TypeScript definition

What category should the rule belong to? [ ] Enforces code style (layout) [ ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [x] Other: Increase type coverage

Provide 2-3 code examples that this rule should warn about:

<script lang="ts">
export default {
  props: {
    total: {
      type: Object, // Error
      required: true,
    },
  },
};
</script>
<script lang="ts">
export default {
  props: {
    total: {
      type: Object as PropType<Price>, // OK
      required: true,
    },
  },
};
</script>

Additional context

Since defineComponent from vue-next or @vue/composition-api TypeScript compiler understands how properties of this are typed. However there's no way to force writing type definition of props (other than noImpliciteThis in the tsconfig). Rule like this can improve TypeScript migrations.

Please let me know what you think about it. If you like this idea I can implement this rule.

przemyslawjanpietrzak avatar Oct 11 '21 20:10 przemyslawjanpietrzak

Thank you for the rule proposal. The rule are interesting, but I'm not familiar with typescript AST. If you open the PR, we can add rule, but it can be difficult to maintain typescript-specific rules with this plugin.

ota-meshi avatar Oct 28 '21 05:10 ota-meshi

@przemyslawjanpietrzak any news ?

tatarysh avatar Aug 30 '22 07:08 tatarysh

@tatarysh I have an implementation here:

  • https://raw.githubusercontent.com/przemyslawjanpietrzak/eslint-plugin-vue-extras/master/lib/rules/type-object-props.js
  • https://raw.githubusercontent.com/przemyslawjanpietrzak/eslint-plugin-vue-extras/master/tests/lib/rules/type-object-props.js

If you need this, the simplest way will be just add to as private eslint plugin

przemyslawjanpietrzak avatar Sep 05 '22 07:09 przemyslawjanpietrzak

@przemyslawjanpietrzak Thanks for sharing! Feel free to submit a pull request to add this to the official eslint-plugin-vue!

FloEdelmann avatar Sep 05 '22 07:09 FloEdelmann

@FloEdelmann Thanks. I'm starting the implementation of PR

przemyslawjanpietrzak avatar Sep 05 '22 18:09 przemyslawjanpietrzak