Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

vue/return-in-computed-property requires return even if Typescript union type is exhausted in switch statement #2142

Copy link
Copy link
Open
@Larsmyrup

Description

@Larsmyrup
Issue body actions

Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have read the FAQ and my problem is not listed.

Tell us about your environment

  • ESLint version: 8.39.0
  • eslint-plugin-vue version: 9.11.0
  • Node version: Problem in both v20.0.0 and v16.14.2
  • Operating System: macOS 13.3.1

Please show your full configuration:

/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution')

module.exports = {
  root: true,
  'extends': [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-typescript'
  ],
  parserOptions: {
    ecmaVersion: 'latest'
  }
}

(Default Vue 3 setup)

What did you do?

<script setup lang="ts">
import { computed } from "vue";

interface Foo {
  category: 'foo'
}

interface Baz {
  category: 'baz'
}

type unionType = Foo | Baz

//Use Math random so typescript can't infer the type
const foo: unionType = Math.random() > 0.5 ? { category: 'foo' } : { category: 'baz' }

const computedStuff = computed(() => {  //This is the line that throws the error
  switch (foo.category) {
    case 'foo':
      return 'foo'
    case 'baz':
      return 'baz'
  }
})

</script>

What did you expect to happen?
Expected ESLint to recognize that the switch exhausts the union type and therefore a value would always be returned

What actually happened?

  17:32  error  Expected to return a value in computed function  vue/return-in-computed-property

ESLint complains and says the computed property is missing a return value.

Repository to reproduce this issue
https://github.com/Larsmyrup/return-in-computed-property-bug-repro

Let me know if i need to elaborate on anything

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.