-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Support TypeScript in vue template #1359
Comments
This will add quite more complexity to the template code, the solution for your problem would be better type inference in the template, with tools such as |
I am also dreaming about ts support in templates. For example it's my favorite part of angular especially when you have large and complex project and need to change something, you mostly don't look into templates, you just change script part and never know if your template part is broken :( |
Vue supports runtime template compilation. Bringing support for TypeScript in templates would mean that you'll get different levels of template support for pre-compiled and post-compiled applications, which in result would complicate things for developers and maintainers. In the example above you could provide better type safety by using methods or computeds instead of inline expressions. |
This is technically doable:
Obviously this won't work for the in-browser build, but runtime and build-time compilations already have different levels of support. Still, I'll need to play with this and |
I think use computed or methods |
I think there is an alternate use case here which is usage of syntax that is in TS but not necessarily in the ES standard yet. Null coalescing and the elvis operator are both things I feel are missing. However, I don't think a blanket "support TS in templates" might be the best solution. The ideal solution would be if we could pass expressions through the same build system as the script section, which would ensure you can use all the same features in templates as in the script. I'm not familiar with the template compiler and it seems like this might be problematic though. |
Recent I got some issues direct or indirect point to there, I'm really look forward to see the implement, and this is a great DX improvement. |
TS support in views would be nice if not almost mandatory when you try to have fully type-checked templates in a complex project. We started using Volar in one of our projects and we have a few errors that need a cast or a null-forgiving Sometimes such casts can be moved into the |
I would like to contribute a sample use case that I encountered today. I was rendering a list of heterogeneous items. <template v-for="i of items">
<a v-if="i.isLink" @click="goToLink(i)">{{ i.linkText }}</a>
<div v-else @click="openGroup(i)>{{ i.groupHeader }}</div>
</template>
Given
In pure TS this would be nicely fixed with The next best thing is probably to simply add a cast: I don't want to go crazy with TS inside templates, but for examples such as this one, I don't see a better solution. |
@jods4 you can do this: <template v-for="i of items">
<a
v-if="'linkText' in i"
@click="'linkText' in i ? goToLink(i) : undefined"
>{{ i.linkText }}</a>
<div
v-else
@click="'linkText' in i ? undefined : openGroup(i)"
>{{ i.groupHeader }}</div>
</template> Type narrowing not working in event expression is expected, see vuejs/language-tools#146. |
Is there anyway to disable type checking within the template with Volar until this feature is implemented? It's certainly a pain not being able to address certain type issues without tools like the non-null assertion operator. |
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
setup() {
return {
asAny: (v: any) => v
}
}
});
</script>
<template v-for="i of items">
<a v-if="asAny(i).isLink" @click="goToLink(asAny(i))">{{ asAny(i).linkText }}</a>
<div v-else @click="openGroup(asAny(i))>{{ asAny(i).groupHeader }}</div>
</template> 👀 |
Thank you for the work-around suggestion. |
@jods4 Can you like to where you read that? Curious to know more. |
@jods4 where did you read this? I'm really waiting for TS in templates... |
I guess this does not supported
|
I am not sure if this is supported. But if it is, the syntax should be: |
Dumb mistake 🤦 yes it does work, thanks! |
I just realized I can close this :D |
Hey @yyx990803 is there a way to annotate a prop with a generic so that the types get carried over through scoped slots? CompA
CompB
|
Oh just saw this issue: #3102 |
Volar yes? Similar question vuejs/language-tools#439 |
There seems to be an issue with
This can be reproduced with something as simple as:
(Notice the |
@Spittal The solution for me was to delete |
Hey @avenmore Thanks for the tip unfortunately I can still replicate the issue. I have double and triple checked that I am on |
This is presumably an issue on |
In an environment without ESLint this still throws. I'm using the
and I still get the error. Even on a |
Sorry for the confusion, my comment wasn't related to yours |
No problem, for anyone else looking for the recreation, here is a repo. |
This works fine for me now that didn't work before |
Hm, yeah it seems that 3.2.19 did fix most syntax, but not the non-null assertion operator at the end of a name. |
Please report bugs for TS in templates as separate issues with reproductions. Do not reuse closed issues. |
What problem does this feature solve?
Support TypeScript expression in template
What does the proposed API look like?
The text was updated successfully, but these errors were encountered: