question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hard/Impossible to find the right generic type for the wrapper (Vue3/TS/VTU2)

See original GitHub issue

Problem

When using a variable that you’ll mount to later, the type for the variable is impossible to get for beginners of TS, leading to either no typings at all, or (worse) any typings on the wrapper.

Example

describe('Heading', () => {
  // People are probably able to see VueWrapper is needed here, but they don't know what the generic is supposed to be
  let wrapper

  beforeEach(() => {
    wrapper = mount(Heading)
  })
})

The following leads to no typings at all because of <any>

describe('Heading', () => {
  let wrapper: VueWrapper<any>

  beforeEach(() => {
    wrapper = mount(Heading)
  })
})

The correct generic is ComponentPublicInstance (in this case). But that doesn’t show up in auto-complete and I found it pretty much just through trial and error.

describe('Heading', () => {
  let wrapper: VueWrapper<ComponentPublicInstance>

  beforeEach(() => {
    wrapper = mount(Heading)
  })
})

Solution

I really don’t know enough about TS to offer any kind of help, but I think at least some directions in the documentation could help new users out that have been stuck on this issue.

My main grief with this has been the push for TS with Vue3, which I understand and even applaud. But then immediately getting stuck on pretty much the first step is quite deflating and I’ve seriously considered dropping TS just because I thought the eco-system (as delivered by vue-cli) simply wasn’t ready yet.

Hope this helps and let me know if you guys need any more info!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
afontcucommented, Dec 19, 2020

Closing this as there’s nothing inherently wrong with the test utils, and setting the initial wrapper value before beforeEach works as expected.

Thank you all!

2reactions
TheDutchCodercommented, Dec 14, 2020

Hey guys, thanks for responding to this issue, I appreciate it!

@cexbrayat thanks for the explanation, I understand how difficult TS is/can be in those situations. I think the main issue I’m having with all of this is that on the one hand SFC’s + TS get pushed quite a bit by the Vue team (rightfully so in most cases), yet it seems to pose some problems (like this one) in the tooling.

I haven’t had a deep dive into TS + Vue3, but what I find interesting is that Vetur (the VSCode plugin), seems to have no issue with .vue files. Doesn’t Vetur also just lean on a TS compiler for this? So I wonder what the difference is?

But let’s move this to the discussion instead, so I don’t litter this issue 😃

@pikax That’s an excellent point, that does work indeed, so that’s a great way to make it all work, it’s just not a very intuitive way to write TS/JS I would say, but in this case it’s certainly a good option, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hard/Impossible to find the right generic type for the wrapper ...
Problem When using a variable that you'll mount to later, the type for the variable is impossible to get for beginners of TS, ......
Read more >
Object generic return value (wrapper class) won't allow for ...
I think that intGrid.getElement(0,0) is returning an Integer Java object. The class Integer does not extends YourNodeClass with the ...
Read more >
Generics in Java - YouTube
In a nutshell, generics enable types (classes and interfaces) to be ... easier than fixing runtime errors, which can be difficult to find....
Read more >
Generics In Java - Full Simple Tutorial - YouTube
Full tutorial for using Generics in Java!☕ Complete Java course: https://codingwithjohn.thinkific.com/courses/java-for-beginnersGenerics in ...
Read more >
C++ Type Erasure: Wrapping Any Type | by Debby Nirwan
Understanding how to write a wrapper of any type in a class in C++ to increase the level of abstraction in our code....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found