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

Types don't seem to be narrowed inside of event handlers #269

Closed
shroudedcode opened this issue Jun 16, 2021 · 3 comments
Closed

Types don't seem to be narrowed inside of event handlers #269

shroudedcode opened this issue Jun 16, 2021 · 3 comments
Labels
question Further information is requested

Comments

@shroudedcode
Copy link

Volar already does a good job at narrowing types (from T | undefined to just T) inside of text interpolations ({{ ... }}) and attribute bindings (like :example="...") that are in some way wrapped in a v-if (or another "control flow directive"), but it doesn't seem to do any type narrowing for expressions inside of event handlers (like @example="...").

Here's a small component for reproducing the issue:

<script setup lang="ts">
import { ref } from 'vue'

const foo = ref<{ bar: 'Hello' }>()
</script>

<template>
  <div>
    <!-- `foo` properly narrowed inside of the curly braces -->
    <div v-if="foo">
      Test 1: {{ foo.bar }}
    </div>

    <!-- `foo` properly narrowed inside of `:title` -->
    <div v-if="foo" :title="foo.bar">
      Test 2
      <!-- Also works here -->
      <span :title="foo.bar">Test 2 nested</span>
    </div>

    <!-- `foo` still possibly `undefined` inside of `@click` -->
    <div v-if="foo" @click="alert(foo.bar)">
      Test 3
      <!-- Same problem here -->
      <span @click="alert(foo.bar)">Test 3 nested</span>
    </div>
  </div>
</template>

Apart from tiny issues like this Volar works really well for me by the way, keep up the great work! 🙌

@johnsoncodehk
Copy link
Member

This is expected behavior, some discussion here: #146

@johnsoncodehk johnsoncodehk added the question Further information is requested label Jun 17, 2021
@shroudedcode
Copy link
Author

Ah, makes sense! Guess I'm waiting for TypeScript support in templates then. 😄

@abaumg
Copy link

abaumg commented Aug 29, 2022

For the sake of completeness: This seems to be resolved in #1249.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants