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

Unexpected errors regarding to "v-if" in vue-tsc #1205

Closed
tsekka opened this issue Apr 16, 2022 · 7 comments
Closed

Unexpected errors regarding to "v-if" in vue-tsc #1205

tsekka opened this issue Apr 16, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@tsekka
Copy link

tsekka commented Apr 16, 2022

Here's an example:

<script setup lang='ts'>
class ExampleClass {
  doSomething() {
    // ..
  }
}

const shouldBeNull = Math.round(Math.random()) === 0
const exampleObject: null | ExampleClass = shouldBeNull ? null : new ExampleClass

// As expected, this is fine and will not give any error
if (exampleObject !== null) {
  exampleObject.doSomething()
}

</script>

<template>
  <!-- 
    Everything OK on vscode, but on console I see:
      "src/ExampleComponent.vue:22:49 - error TS2531: Object is possibly 'null'." 
    -->
  <button v-if="exampleObject !== null" @click="exampleObject.doSomething()">Do it</button>
</template>

What can cause this?

@johnsoncodehk
Copy link
Member

Duplicate of #1134

@johnsoncodehk johnsoncodehk marked this as a duplicate of #1134 Apr 20, 2022
@johnsoncodehk johnsoncodehk added the duplicate This issue or pull request already exists label Apr 20, 2022
@tsekka
Copy link
Author

tsekka commented Apr 21, 2022

So, it's intentional that it shows error in console but not in editor?

@johnsoncodehk
Copy link
Member

@tsekka It should show on vue-tsc and vscode both. Are you using same vue-tsc and volar extension version?

@tsekka
Copy link
Author

tsekka commented Apr 21, 2022

@johnsoncodehk Both vue-tsc npm package and volar VSCode extension are 0.34.9.

@tsekka
Copy link
Author

tsekka commented Apr 21, 2022

@johnsoncodehk Both vue-tsc npm package and volar VSCode extension are 0.34.9.

Actually I've just updated vue-tsc to 0.34.9. It was 0.33 before. And it's not showing error on console anymore.

@johnsoncodehk
Copy link
Member

It should report an error, will fix in next version. You should change you code to @click="exampleObject!.doSomething()".

@tsekka
Copy link
Author

tsekka commented Apr 21, 2022

@johnsoncodehk Thanks.

@johnsoncodehk johnsoncodehk reopened this Apr 21, 2022
@johnsoncodehk johnsoncodehk added bug Something isn't working and removed duplicate This issue or pull request already exists labels Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants