Skip to content
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

vue/require-default-prop gives a false error with " as PropOptions<T>" with TypeScript #564

Closed
wuservices opened this issue Aug 29, 2018 · 2 comments

Comments

@wuservices
Copy link

Tell us about your environment

  • ESLint Version: 4.19.1
  • eslint-plugin-vue Version: 4.7.1
  • Node Version: 10.9.0

Please show your full configuration:

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: ["plugin:vue/recommended", "@vue/prettier", "@vue/typescript"],
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
  },
  parserOptions: {
    parser: "typescript-eslint-parser"
  }
};

What did you do? Please include the actual source code causing the issue.

  1. Create new TypeScript project with ESLint + Prettier with Vue CLI 3.0.1
  2. In .eslintrc.js, change plugin:vue/essential to plugin:vue/recommended to get all rules
  3. Add PropOptions types to HelloWorld.vue as seen below. In this example I use PropOptions<string>, which is redundant (but still shows the problem). In practice, this would be a more complex type like an interface. Typings with Typescript 2.7.1 vue#7640 (comment) is where I got inspiration for this typing syntax.
<script lang="ts">
import Vue, { PropOptions } from "vue";

export default Vue.extend({
  props: {
    msg: {
      type: String,
      required: true
    } as PropOptions<string>
  }
});
</script>

What did you expect to happen?
No errors that prop msg requires a default value to be set.

$ yarn lint
yarn run v1.9.4
$ vue-cli-service lint
 DONE  No lint errors found!

What actually happened? Please include the actual, raw output from ESLint.

It looks like adding as PropOptions<string> trips up the parser such that it doesn't recognize that the msg prop is required and therefore doesn't need a default value

$ yarn lint
yarn run v1.9.4
$ vue-cli-service lint
error: Prop 'msg' requires default value to be set (vue/require-default-prop) at src/components/HelloWorld.vue:89:5:
  87 | export default Vue.extend({
  88 |   props: {
> 89 |     msg: {
     |     ^
  90 |       type: String,
  91 |       required: true
  92 |     } as PropOptions<string>
@michalsnik
Copy link
Member

Thanks for posting this issue @wuservices I really like how you explained the problem. I confirmed it and it's due to the fact that typescript-eslint-parser returns an additional wrapper node in AST that I didn't thought about earlier. It's going to be a quick fix :)

@michalsnik michalsnik self-assigned this Sep 13, 2018
armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Oct 17, 2018
armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Oct 17, 2018
* Add "as" helper
* Fix vue/require-default-prop
* Fix vue/require-prop-types
* Allow to use export default (Vue as VueConstructor<Vue>).extend({ syntax
Fix issues: vuejs#564 vuejs#575
armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Oct 17, 2018
* Add "as" helper
* Fix vue/require-default-prop
* Fix vue/require-prop-types
* Allow to use `export default (Vue as VueConstructor<Vue>).extend({` syntax

Fix issues: vuejs#564 vuejs#575
armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Oct 19, 2018
* Add "as" helper
* Fix vue/require-default-prop
* Fix vue/require-prop-types
* Allow to use `export default (Vue as VueConstructor<Vue>).extend({` syntax

Fix issues: vuejs#564 vuejs#575
armano2 added a commit to armano2/eslint-plugin-vue that referenced this issue Oct 20, 2018
* Add "as" helper
* Fix vue/require-default-prop
* Fix vue/require-prop-types
* Allow to use `export default (Vue as VueConstructor<Vue>).extend({` syntax

Fix issues: vuejs#564 vuejs#575
michalsnik pushed a commit that referenced this issue Nov 6, 2018
* Add typescript specific changes

* Add "as" helper
* Fix vue/require-default-prop
* Fix vue/require-prop-types
* Allow to use `export default (Vue as VueConstructor<Vue>).extend({` syntax

Fix issues: #564 #575

* Fix vue/require-prop-type-constructor

* Fix vue/require-valid-default-prop

* Add more tests in utils
@armano2
Copy link
Contributor

armano2 commented Nov 6, 2018

@michalsnik can you close this ticket ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants