Skip to content

Narrowed type by v-if should not be widened in v-on etc. #1212

@ktsn

Description

@ktsn

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions