Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a function that throws an error in Typescript

Tags:

typescript

In Java I would declare a function like this:

public boolean Test(boolean test) throws Exception {   if (test == true)     return false;   throw new Exception(); } 

And I can use this function without handling the exception.

If it is possible, how to do the same in Typescript? The compiler will tell me that I can't use the function without a try/catch.

like image 822
The224 Avatar asked Mar 22 '18 17:03

The224


People also ask

How do you use throw error?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

What's the difference between throw error (' MSG ') vs throw new error (' MSG ')?

catch statement with having throw Error(). This code will do that the 'msg' error is thrown in the try-block which will be get executed by catch statements. new Error object: It captures several properties of the place where the error happened. It exposes an error event with two parameters name & message.

How do you throw an error in JavaScript?

Throwing a generic exception is almost as simple as it sounds. All it takes is to instantiate an exception object—with the first parameter of the Error constructor being the error message—and then… "throw" it.


2 Answers

There is no such feature in TypeScript. It's possible to specify error type only if a function returns an error, not throws it (this rarely happens and is prone to be antipattern).

The only relevant type is never. It is applicable only if a function definitely throws an error, it cannot be more specific than that. It's a type as any other, it won't cause type error as long as it doesn't cause type problems:

function Test(): never => {   throw new Error(); }  Test(); // won't cause type error let test: boolean = Test(); // will cause type error 

When there is a possibility for a function to return a value, never is absorbed by return type.

It's possible to specify it in function signature, but for reference only:

function Test(test: boolean): boolean | never {   if (test === true)     return false;    throw new Error(); } 

It can give a hint to a developer that unhandled error is possible (in case when this is unclear from function body), but this doesn't affect type checks and cannot force try..catch; the type of this function is considered (test: boolean) => boolean by typing system.

like image 163
Estus Flask Avatar answered Sep 20 '22 00:09

Estus Flask


You can mark the function with @throws jsdoc at least. Even though it does not provide static analysis errors in typescript compiler, some good IDE or linter may still report a warning if you try to disregard the function that throws...

/**   * @throws {Error}  */ function someFunc() {     if (Math.random() < 0.5) throw Error(); } someFunc(); 

enter image description here

like image 21
Klesun Avatar answered Sep 22 '22 00:09

Klesun