You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would be great to have a way to define the type of error(s) a function or method can throw
π Motivating Example
Safer error handling and dev experience on exceptions
Type-safe error handling would improve the code experience, giving faster feedback on which kind of exception is possible or even with a catch error type inference.
π» Use Cases
What do you want to use this for?
basic throw annotation:
classMyCustomErrorextendsError{constructor(msg: string,publiccode: number){super(msg)this.name=MyCustomError.name}}functionfailableFn(code: number): numberthrowMyCustomError{// <- hereif(code<0)thrownewMyCustomError('Bad code!',code)returncode}functiondoStuff(code: number){try{console.log('Code: ',failableFn(code))}catch(ex){// inferred as MyCustomErrorconsole.log(`Failure for ${code}: ${ex.message}`)}}console.log(doStuff(1))// Code: 1console.log(doStuff(-1))// "Failure for -1: Bad code!"
I think ideally the throw could even be optional, as I believe the compile could infer the throw types in the function body.
What shortcomings exist with current approaches?
Feels like a hole in the possible very strict type check philosophy of TypeScript
What workarounds are you using in the meantime?
We have to manually type check the exception or weak type it
π Search Terms
"thows", "typed error handling", "strict error"
β Viability Checklist
β Suggestion
Would be great to have a way to define the type of error(s) a function or method can throw
π Motivating Example
Safer error handling and dev experience on exceptions
Type-safe error handling would improve the code experience, giving faster feedback on which kind of exception is possible or even with a
catch
error type inference.π» Use Cases
basic throw annotation:
I think ideally the
throw
could even be optional, as I believe the compile could infer thethrow
types in the function body.Feels like a hole in the possible very strict type check philosophy of TypeScript
We have to manually type check the exception or weak type it
The text was updated successfully, but these errors were encountered: