typeGrade='A+'|'A'|'B+'|'B'|'C'|'D'|'F';functionstudentFinalResult(grade:Grade):string{switch(grade){case'A+':return'Nailed It! 🥳';case'A':case'B+':case'B':return'Passed 💃';case'C':return'Barely Survived 😌';case'D':case'F':return'Failed 😢';default:const_exhaustiveCheck:never=grade;// "Type 'string' is not assignable to type 'never'".return_exhaustiveCheck;// if one of the union type values is missing}}console.log(studentFinalResult('A+'));// "Nailed It! 🥳"
TypeScript supports exhaustiveness checking on union types.
playground