FG
🌐 Web & Full-StackMicrosoft

Suggestion: `throws` clause and typed catch clause

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

The typescript type system is helpful in most cases, but it can’t be utilized when handling exceptions. For example: [code block] The problem here is two fold (without looking through the code): 1. When using this function there’s no way to know that it might throw an error 2. It’s not clear what the type(s) of the error is going to be In many scenarios these aren't really a problem, but knowing whether a function/method might throw an exception can be very useful in different scenarios, especially when using different libraries. By introducing (optional) checked exception the type system can be utilized for exception handling. I know that checked exceptions isn't agreed upon (for example Anders Hejlsberg), but by making it optional (and maybe inferred? more later) then it just adds the opportunity to add more information about the code which can help developers, tools and documentation. It will also allow a better usage of meaningful custom errors for large big projects. As all javascript runtime errors are of type Error (or extending types such as `TypeError`) the actual type for a function will always be `type | Error`. The grammar is straightforward, a function definition can end with a throws clause followed by a type: [code block] When catching the exceptions the syntax is the same with the ability to declare the type(s) of the error: `catch(e: string | Error) { ... }` Examples: [code block] Here it’s clear that the function can throw an error and that the e

Error Output

error: can't deal with 0";

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Optional Throws Clause and Typed Catch in TypeScript

Medium Risk

TypeScript's current exception handling lacks a mechanism to specify which exceptions a function may throw, leading to ambiguity in error handling. This makes it difficult for developers to understand potential errors and handle them appropriately, especially in large projects with multiple libraries.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define Function with Throws Clause

    Modify function definitions to include an optional 'throws' clause that specifies the type of error that may be thrown. This will enhance clarity for developers using the function.

    typescript
    function exampleFunction(param: number): void throws TypeError | RangeError { /* implementation */ }
  2. 2

    Implement Typed Catch Clause

    Update the catch blocks in your code to specify the type of error being caught. This will allow for more precise error handling and improve code readability.

    typescript
    try { exampleFunction(0); } catch (e: TypeError | RangeError) { console.error(e.message); }
  3. 3

    Update Documentation

    Revise the documentation for functions that now include the throws clause to clearly state what errors may be thrown. This will help other developers understand how to use the function correctly.

    typescript
    /**
     * @throws TypeError if param is not a number
     * @throws RangeError if param is out of range
     */
  4. 4

    Conduct Code Review

    Perform a code review to ensure that all functions have been updated to include the throws clause and that catch blocks are typed correctly. This will help maintain code quality and consistency across the project.

  5. 5

    Run Unit Tests

    Create or update unit tests to cover scenarios where the function may throw errors. This will ensure that the new error handling is functioning as expected.

    typescript
    it('should throw TypeError for invalid input', () => { expect(() => exampleFunction(0)).toThrow(TypeError); });

Validation

Confirm that the functions now include the throws clause and that the catch blocks are typed correctly. Additionally, run the unit tests to ensure that the expected errors are thrown and handled appropriately.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typescriptcompilersuggestiondeclined