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?
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With