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.

Template interpolation incorrect type on event (Typescript)

See original GitHub issue
  • I have searched through existing issues
  • I have read through docs
  • I have read FAQ
  • I have tried restarting VS Code or running Vetur: Restart VLS

Info

  • Platform: Windows
  • Vetur version: 0.30.3
  • VS Code version: 1.51.1

Problem

Vetur is reporting false error with "vetur.experimental.templateInterpolationService": true due to inccorect typing when passing a variable from an event, example:

  <!-- foo is computed property with return type number | undefined -->

  <div v-if="foo">
    <!-- foo now should be number  -->
    {{ foo }} <!-- Correct type (number) -->
    <input :example="foo" /> <!-- Also correct type (number) -->
    <input type="text" @input="bar(foo)" /> <!-- Incorrect type (number | undefined) -->
  </div>

image

Reproducible Case

<template>
  <div v-if="foo">
    <!-- foo now should only be number  -->
    {{ foo }} <!-- Correct type (number) -->
    <input :example="foo" /> <!-- Also correct type (number) -->
    <input type="text" @input="bar(foo)" /> <!-- Incorrect type (number|undefined) -->
  </div>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";

@Component
export default class Test extends Vue {
  get foo(): number | undefined {
    return 1;
  }

  bar(x: number) {
    return x * 2;
  }
}
</script>

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
ktsncommented, Nov 30, 2020

#1212

I’ve been thinking of this problem and there seem to be two approaches:

  1. Change template transformer implementation to like what Angular does (use if statement and user defined type guard to generate context, in this case $event, instead of using helper function and callback) so that we can eliminate callbacks from transformed code.

https://medium.com/angular-in-depth/type-checking-templates-in-angular-viewengine-and-ivy-77f8536359f5

  1. Assign all values in if condition into local variables so that we avoid type widening.
if (vm.foo.bar.baz) {
  const __foo = vm.foo
  const __foo_bar = vm.foo.bar
  const __foo_bar_baz = vm.foo.bar.baz

  {
    on: {
      input: $event => func(__foo_bar_baz)
    }
  }
}

The latter approach should be easier than the former but I’m not sure if it actually works properly. There might be some edge cases that I’m not aware of.

0reactions
yoyo930021commented, Nov 30, 2020

Duplicate of #1212

Read more comments on GitHub >

github_iconTop Results From Across the Web

Text Interpolation is not updating in angular when an ...
I know the postMessage eventListener is working because when the parent does a console.log(JSON.stringify(event)) it logs as expected. The event ...
Read more >
Documentation - Template Literal Types - TypeScript
When a union is used in the interpolated position, the type is the set of every possible string literal that could be represented...
Read more >
Template type checking - Angular
Just as TypeScript catches type errors in your code, Angular checks the expressions and bindings within the templates of your application and can...
Read more >
Template literals (Template strings) - JavaScript | MDN
The string text that will become part of the template literal. ... Dollar signs can be escaped as well to prevent interpolation.
Read more >
Interpolation Support | Vetur - GitHub Pages
Vue template allows JavaScript-esque expression in four constructs: ... also only disable template diagnostics with vetur.validation.interpolation: false .
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