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.

vue 2.7 template type check for inline function

See original GitHub issue

I’m using vue 2.7 with TypeScript

<template>
<div>
  <div v-for="(item, index) in arr" :key="index">
    <el-input type="text" :value="item" @input="(val) => doSomething(index, val)" />
  </div>
</div>
</template>

As code shown above, the problem is @input="(val) => doSomething(index, val)" part.

  • ts error Parameter 'val' implicitly has an 'any' type. since noImplicitAny in tsconfig was enabled
  • Vue 2.7 doesn’t support ts syntax in template as the CHANGELOG said

    ❌ TypeScript syntax in template expressions (incompatible w/ Vue 2 parser)

  • ts-ignore will causing prettier add leading semi colon which is not valid attribute value
    @input="
     //@ts-ignore
     ;(val) => doSomething(index, val)
    "
    
  • Since val is an argument so asAny magic won’t work

Any workaround I can do?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:25 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lsdsjycommented, Aug 1, 2022

I see. Instead of @ts-ignore you can use something similar to the asAny trick:

@input="anyFn((val) => doSomething(index, val))"

, where const anyFn = (fn: (val: any) => any) => fn. And you can annotate it more accurately if you want.

0reactions
troy351commented, Sep 26, 2022

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue-template-compiler - npm
This package can be used to pre-compile Vue 2.0 templates into render functions to avoid runtime-compilation overhead and CSP restrictions.
Read more >
Template Refs - Vue.js
When the element is unmounted, the argument will be null . You can, of course, use a method instead of an inline function....
Read more >
Using Vue with TypeScript - Vue.js
To use TypeScript in SFCs, add the lang="ts" attribute to <script> tags. When lang="ts" is present, all template expressions also enjoy stricter type...
Read more >
Class and Style Bindings - Vue.js
A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can...
Read more >
vue/no-unsupported-features
disallow unsupported Vue.js syntax on the specified version. ... Vue.js 2.7.0+. "style-css-vars-injection" ... SFC CSS variable injection ...
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