Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[switch-exhaustiveness-check] ban the default case to enforce all cases are handled and prevent accidental mishandling of new cases #3616

Closed
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look
@OliverJAsh

Description

@OliverJAsh

I frequently see code like this:

typeMyUnion='a'|'b';declareconstmyUnion:MyUnion;constf1=()=>(myUnion==='a' ?'a' :'b');f1('a')// 'a'f1('b')// 'b'

The logic inf1 makes an assumption: ifmyUnion is nota, it must beb.

Later on, someone might update theMyUnion type and this assumption will breakdown:

-type MyUnion = 'a' | 'b';+type MyUnion = 'a' | 'b' | 'c';

The runtime behaviour is clearly incorrect, yet TypeScript will not error to remind us that we need to update the logic inf1:

f1('a')// 'a'f1('b')// 'b'f1('c')// 'b' ❌

This problem is not specific to the ternary operator but alsoif andswitch statements:

constf2=()=>{if(myUnion==='a'){return'a';}else{return'b';}};constf3=()=>{switch(myUnion){case'a':return'a';default:return'b';}};

As we can see, it is not safe to make assumptions about the value that reaches theelse/default case because it can change.

Instead we need to explicitly specify all cases:

importassertNeverfrom'assert-never';typeMyUnion='a'|'b';declareconstmyUnion:MyUnion;constf2=()=>{if(myUnion==='a'){return'a';}elseif(myUnion==='b'){return'b';}else{assertNever(myUnion);}};constf3=()=>{switch(myUnion){case'a':return'a';case'b':return'b';}};constf3b=()=>{switch(myUnion){case'a':return'a';case'b':return'b';default:assertNever(myUnion);}};

This way, when the type is eventually widened, TypeScript will generate a type error so we're reminded that we need to update our code:

importassertNeverfrom'assert-never';typeMyUnion='a'|'b'|'c';declareconstmyUnion:MyUnion;constf2=()=>{if(myUnion==='a'){return'a';}elseif(myUnion==='b'){return'b';}else{// Argument of type 'string' is not assignable to parameter of type 'never'.assertNever(myUnion);}};//@noImplicitReturns: true// Not all code paths return a value.constf3=()=>{switch(myUnion){case'a':return'a';case'b':return'b';}};constf3b=()=>{switch(myUnion){case'a':return'a';case'b':return'b';default:// Argument of type 'string' is not assignable to parameter of type 'never'.assertNever(myUnion);}};

I would like to propose a rule that enforces this. The rule would report an error inside a ternary orif/switch statement if we're switching over a union type (exceptboolean) and we have a fallback case (else/default). The fix would be to explicitly specify all cases.

I'm really not sure what we would call it.

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp