Skip to content

Does function throw? Ability to tell if a function could throw by looking at its signature.Β #55614

Closed

Description

πŸ” Search Terms

Throw
Function signature

βœ… 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

When looking at functions in typescript. We can see what arguments it takes and their types as well as the type it will return. But what we don't know is if anywhere down the line of this function is if it uses the throw keyword. If it does and we don't know about it then this can very much change the behaviour of our resumed predictable code.

My suggestion is that when looking at the function signature, it indicates in some method where it could throw or not. This flag would bubble up to any function that uses the function that could throw unless wrapped inside a try catch block.

This should also apply to functions like fetch, which doesn't actually have the throw keyword in it, but can still throw.

πŸ“ƒ Motivating Example

This feature can help developers know and plan where and when to wrap stuff in try catch blocks to catch this undefined behaviour that somewhere further down the code breaks for an unexpected reason. The throw keyword is not a mistake of JavaScript. It is a feature and we should have better features to know when it has been used.

πŸ’» Use Cases

  1. What do you want to use this for?
    To know if a function may break and stop it from breaking the rest of my code, without adding unnecessary try catch blocks.
  2. What shortcomings exist with current approaches?
    We currently have no way to know if a function may throw, with the exception of documentation or looking at source code.
  3. What workarounds are you using in the meantime?
    Assuming these are fine until they break with a new unknown error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions