Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed error handling #55639

Closed
5 tasks done
lucasteles opened this issue Sep 5, 2023 · 2 comments
Closed
5 tasks done

Typed error handling #55639

lucasteles opened this issue Sep 5, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@lucasteles
Copy link

πŸ” Search Terms

"thows", "typed error handling", "strict error"

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ 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

  1. What do you want to use this for?

basic throw annotation:

class MyCustomError extends Error {
  constructor(msg: string, public code: number) {
    super(msg)
    this.name = MyCustomError.name
  }
}

function failableFn(code: number): number throw MyCustomError { // <- here
    if (code < 0)
      throw new MyCustomError('Bad code!', code)
    return code
}

function doStuff(code: number) {
    try{
      console.log('Code: ', failableFn(code))
    }
    catch (ex) { // inferred as MyCustomError
      console.log(`Failure for ${code}: ${ex.message}`)
    }
}
console.log(doStuff(1)) // Code: 1
console.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.

  1. What shortcomings exist with current approaches?

Feels like a hole in the possible very strict type check philosophy of TypeScript

  1. What workarounds are you using in the meantime?

We have to manually type check the exception or weak type it

function failableFn(code: number): number {
    if (code < 0)
      throw new MyCustomError('Bad code!', code)
    return code
}

function doStuff(code: number) {
    try{
      console.log('Code: ', failableFn(code))
    }
    catch (ex) { // unknown
      if (ex instanceof MyCustomError)
          console.log(`Failure for ${code}: ${ex.message}`)
      else
         console.error(`other? `, ex)
    }
}
@MartinJohns
Copy link
Contributor

Duplicate of #23060 / #13219 and others.

The last time it was suggested... two days ago: #55614.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 6, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants