-
-
Notifications
You must be signed in to change notification settings - Fork 595
Open
Description
The current v-if narrowing implementation (#1208), there are some cases the narrowed type is widened to the original type.
<template>
<div v-if="post">
<!-- post is of type "Post" here -->
<!-- post will be widened to "Post | null" in @click directive -->
<button @click="onClick(post)">Click</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
interface Post {
id: string
}
export default Vue.extend({
data() {
return {
post: null as Post | null
}
},
methods: {
onClick(post: Post) {
// ...
}
}
})
</script>This is because v-on emits callback code and TypeScript widens the narrowed type in a callback. We may need to assign some local variable to retain narrowed types.
dodas, kimamula, yoyo930021, alex7229 and hfhchan