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>
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:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
#1212
I’ve been thinking of this problem and there seem to be two approaches:
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
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.
Duplicate of #1212