Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have consistent-return with exhaustive switch-case statements?

I have code like this:

function (flavors: IceCreamFlavor): 'like'|'dislike' {
    switch (flavors) {
        case IceCreamFlavor.vanilla:
            return 'dislike';
        case IceCreamFlavor.chocolate:
            return 'like';
    }
}

This is exhaustive, and I have an eslint rule that ensures exhaustiveness.

For eslint / typescript, i have the consistent-return rule switched to on, which will complain during this. I don't want to add a default (b/c if I add a new ice cream flavor, i want the developer to have to handle it), but consistent-return doesn't realize this is exhaustive and will complain.

Any ideas on how I can handle this elegantly?

like image 920
Willy Xiao Avatar asked Jan 25 '26 15:01

Willy Xiao


2 Answers

Okay, I figured out the problem with my question.

consistent-return comes from eslint, which doesn't know about types, so there's no great way of "fixing" consistent-return to know about this.

Probably the better way of doing it is to disable consistent-return and then to use typescript-lint's explicit-function-return-type or even no-unsafe-return or something like that.

Because that would be really costly to move the whole codebase over to strongly typed functions, you could contribute a tslint rule that does consistent-return that does know about types, but if you're using ts, you probably just want to remove consistent-return.

like image 68
Willy Xiao Avatar answered Jan 28 '26 14:01

Willy Xiao


Probably way too late to help OP, but for any future readers: the TS config option noImplicitReturns gives all the benefits of ESLint's consistent-return, while also supporting exhaustive switches/etc. because it's type-aware.

for reference:

  • https://blog.logrocket.com/exploring-advanced-compiler-options-typescript/#noimplicitreturns
  • https://www.typescriptlang.org/tsconfig#noImplicitReturns
like image 44
cypherfunc Avatar answered Jan 28 '26 16:01

cypherfunc