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

When using shorthand function initializer on computed, this.foo shows error #1175

Closed
3 tasks done
jtsom opened this issue Apr 2, 2019 · 21 comments
Closed
3 tasks done

Comments

@jtsom
Copy link

jtsom commented Apr 2, 2019

  • I have searched through existing issues
  • I have read through docs
  • I have read FAQ

Info

  • Platform: MacOS
  • Vetur version: 0.18.0
  • VS Code version: Version 1.33.0-insider (1.33.0-insider)

Problem

In a Vue application that is using just plain Javascript (<script>), setting

  "compilerOptions": {
    "checkJs": true
  }

in jsconfig.json to enable the new features mentioned in 0.18.0 ( "compilerOptions": {
"checkJs": true
})
causes many things that were errors to show up as errors.

  1. imports can't be found using the '@' shortcut:

image

image

Variables defined in a data() section are not recognized:
image

image

Reproducible Case

Create a new vue (cli) app, add some variables in a data statement, see the errors.
Try adding an import with a @.

@octref
Copy link
Member

octref commented Apr 2, 2019

@octref octref closed this as completed Apr 2, 2019
@octref octref added the question label Apr 2, 2019
@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

That’s fine but I don’t think that will help the issue with not recognizing the variables defined in the data() section, nor the getters defined my the mapGetters/mapState

@octref
Copy link
Member

octref commented Apr 2, 2019

Can you post a minimal reproducible example. Like the source of a full SFC.
data needs to return an object, so const foo = 5 aren't bound to this instance. return { foo: 5 } is.

@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

This is the complete data statement:

image

So yes, it returns an object.

@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

And the actions:
image

And the getters (computed):
image

@octref
Copy link
Member

octref commented Apr 2, 2019

By "minimal reproducible example" I meant put your project generated with vue-cli in a public GH repo.
Minimal in that "make smallest edits from the generated code to show the problem".
I can't figure out how the rest of your file look like and how your whole project is setup.

@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

Here you go.

https://github.com/jtsom/VeturTest

See the lines marked // NOT RECOGNIZED

@octref
Copy link
Member

octref commented Apr 2, 2019

I wouldn't expect cardData to work, but this is surprising:

lol

Also once you change the way you write computed, it started to work:

image

I guess the issues are related to the typing upstream.

@octref octref reopened this Apr 2, 2019
@octref octref changed the title Turning on "checkJs" in JS due project shows many errors When using shorthand function initializer on computed, this.foo shows error Apr 2, 2019
@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

I’m not seeing how you changed the way computed was written?

@jtsom
Copy link
Author

jtsom commented Apr 2, 2019

And why wouldn’t “cardData” work? How would I make it work (not give an error)? Short of ignoring the error...

@octref
Copy link
Member

octref commented Apr 3, 2019

Sorry that's overlapped behind the hover. Try thisIsSelected: function() {.

As for mapState. I didn't find your usage in the docs: https://vuex.vuejs.org/guide/state.html.

@jtsom
Copy link
Author

jtsom commented Apr 3, 2019

@p-kuen
Copy link
Contributor

p-kuen commented Apr 4, 2019

I can confirm that writing computed functions with explicit function() is the only way computed functions work well.
Writing them without them also removes type inference.

Working with TS.

@mrozekma
Copy link

Not sure if it helps, but the computed block parses correctly as long as any entry uses the name: function() {...} syntax; it only breaks if every entry uses the shorthand name() {...}

@octref
Copy link
Member

octref commented Apr 25, 2019

Upstream issue:

microsoft/TypeScript#30854

See this comment. For now you can try to annotate the return type with JSDocs:

image

@fairking
Copy link

Found this open issue is similar to mine. Any idea how to fix all of this guys?
image

@p-kuen
Copy link
Contributor

p-kuen commented Sep 12, 2019

@denis-pujdak-adm-it
The problem you have is different.
If you are using <script lang="ts">, try using export default Vue.extend and `import Vue from 'vue' at the beginning. That should fix the errors.

@fairking
Copy link

fairking commented Sep 12, 2019

Sorry mate but export default Vue.extend gives me even more errors:
image
But if I remove Vue.extend the situation looks much better:
image

@fairking
Copy link

Nope. The import Vue from "vue" is not working. It is working if I add the line then model errors dissapear, but after I close VS Code and open it again those errors come back.
image

@mrozekma
Copy link

@denis-pujdak-adm-it Patcher56 is correct, you need to use Vue.extend(...) or Typescript can't infer anything about the component options. You're missing the parentheses in your screenshot; it's a function call:

export default Vue.extend({
    data() {
        // etc.
    }
});

@octref
Copy link
Member

octref commented Sep 30, 2019

Should be fixed as we upgraded to TS 3.6.3 in Vetur 0.22.3.

@octref octref closed this as completed Sep 30, 2019
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

5 participants