Skip to content

Utility Error class for performing exhaustiveness checks via exceptions in TypeScript.

License

Notifications You must be signed in to change notification settings

buschtoens/unsupported-value-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unsupported-value-error

CI npm version Download Total code style: prettier
Dependabot enabled dependencies Status devDependencies Status

Little TypeScript utility Error class for performing exhaustiveness checks via exceptions, based on this post by Dr. Axel Rauschmayer.

enum NoYes {
  No = 'no',
  Yes = 'yes'
}

function toGerman(x: NoYes) {
  switch (x) {
    case NoYes.No: return 'Nein';
    // case NoYes.Yes: return 'Ja';
    default: throw new UnsupportedValueError(x);
    // => TS2345: Static TS error: Argument of type 'NoYes.Yes' is not assignable to parameter of type 'never'.
    // => Runtime: Unsupported value: 'yes'
  }
}

About

Utility Error class for performing exhaustiveness checks via exceptions in TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published