How to declare a function that throws an error in Typescript
TypescriptTypescript Problem Overview
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.
Typescript Solutions
Solution 1 - Typescript
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.
Solution 2 - Typescript
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();
Solution 3 - Typescript
It is not possible at this moment. You can check out this requested feature: https://github.com/microsoft/TypeScript/issues/13219
Solution 4 - Typescript
You could treat JavaScript's Error
as Java's RuntimeException
(unchecked exception).
You can extend JavaScript's Error
but you have to use Object.setPrototypeOf
to restore the prototype chain because Error
breaks it. The need for setPrototypeOf is explained in this answer too.
export class AppError extends Error {
code: string;
constructor(message?: string, code?: string) {
super(message); // 'Error' breaks prototype chain here
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
this.name = 'AppError';
this.code = code;
}
}
Solution 5 - Typescript
You cannot using pure ts (v<3.9) I hope it will be available in the future. A workaround is however possible, it consists of hiding the possible thrown types in the method's signature to then recover those types in the catch block. I made a package with this workaround here: https://www.npmjs.com/package/ts-throwable/v/latest
usage is more or less as follow:
import { throwable, getTypedError } from 'ts-throwable';
class CustomError extends Error { /*...*/ }
function brokenMethod(): number & throwable<CustomError> {
if (Math.random() < 0.5) { return 42 };
throw new CustomError("Boom!");
}
try {
const answer: number = brokenMethod()
}
catch(error){
// `typedError` is now an alias of `error` and typed as `CustomError`
const typedError = getTypedError(error, brokenMethod);
}
Solution 6 - Typescript
Not TypeScript, but Hegel might be of interest which is another type-checker for JavaScript, and has this feature. You'd write:
function Test(test: boolean): boolean | $Throws<Exception> {
if (test)
return false;
throw new Exception();
}
Solution 7 - Typescript
This seems like a interesting PR to follow regarding this topic https://github.com/microsoft/TypeScript/pull/40468
This PR introduces:
- A new type-level expression: throw type_expr. Currently throw type only throws when it is being instantiated.
- A new intrinsic type TypeToString to print a type