Declare that a method throws error in typescript?
Asked Answered
C

4

13

The following doesn't compile with error "a function whose declared type is neither void nor any must return a value or consist of a single throw statement".

Is there a way to make the compiler recognize that _notImplemented throws an exception?

function _notImplemented() {
   throw new Error('not implemented');
}

class Foo {
    bar() : boolean { _notImplemented(); }

The only woraround I can see is to use generics. But it seems kind of hacky. Is there a better way?

function _notImplemented<T>() : T {
   throw new Error('not implemented');
}

class Foo {
    bar() : boolean { return _notImplemented(); }
Cottle answered 23/12, 2015 at 16:17 Comment(0)
T
9

You can specify that _notImplemented return the type never. never is a special type for function that never return. It can be because of an infinite loop or because it always throw an error.

function _notImplemented() : never {
     throw new Error("not Implemented")
}
Twenty answered 8/1, 2019 at 21:47 Comment(2)
Yes. Your answer only works if a function ONLY throws. If a function generally returns without Error, but occasionally throws, we cannot use never (that disables the function), but we still want some kind of annotation. That is the point of throws clause.Slipknot
Of course, I assumed the function "_notImplemented" to only throw, as requested by the use case of the OP. The error in the code is not that the throws are not declared, but that notImplemented is expected to return multiple things depending of where it is put. by making it return never, it will correcly fix this problem in a typescript way. of course, it does not document what can be thrown by the function. for this, typescript fall short and using Monadic Either might be a better error management system, when possible.Pendulum
K
7

You can use an Either rather than a throw. An Either is a structure that typically holds either an error or a result. Because it is a type like any other, TypeScript can easily utilize it.

Example:

function sixthCharacter(a: string): Either<Error, string> {
    if (a.length >= 6) {
        return Either.right<Error, string>(a[5]);
    }
    else {
        return Either.left<Error, string>(new Error("a is to short"));
    }
}

A function that utilizes the function sixthCharacter may choose to unwrap it, return a maybe itself, throw an error itself, or other options.

You'll need to select a library that has an Either in it - take a look at monad libraries like TsMonad or monet.js.

Kajdan answered 29/8, 2016 at 14:45 Comment(2)
I'm interfacing with legacy software. I cannot use EitherCottle
Either is comming from some library, you can install a library.Lubber
F
1

AFAIK there is currently no non-hacky way how to deal with this problem.

This is currently being examined by the TypeScript team on github (https://github.com/Microsoft/TypeScript/issues/1042) and we could have some solution soon.

Fechner answered 29/4, 2016 at 7:28 Comment(0)
R
-3

We shouldenter image description here treat throw as equivalent to return for the purposes of this check

Roane answered 21/12, 2022 at 2:46 Comment(1)
I don't see how this answers the question "Is there a way to make the compiler recognize that _notImplemented throws an exception?"Nicky

© 2022 - 2024 — McMap. All rights reserved.