This project is read-only.

Feature Request: type annotations in catch clauses

Topics: Language Specification
Oct 5, 2013 at 3:01 PM
First of all, I really like the direction TypeScript is going (despite a couple of rough edges in the VS plugin with 0.9.x ;) ) and I'm looking forward to the 1.0 release.

I could not find anything about try catch blocks in the spec or on CodePlex but I think it might be interesting to talk about typed catch clauses like in C#, C++ or what not. The compiler ( does currently not allow to add a type annotation to a catch clause (TS1013), so for example...
try {
    // ...
} catch(err: IException) {
    // ...
... will not work.

I'm aware of the fact that - in javascript - try blocks should ideally contain function calls only as try blocks are slow in todays JITs, but catch blocks can often get quite large, so it would be nice if one didn't have to cast the exception all the way down by hand.

Currently, the way to get a statically typed exception would be wrapping code into a function, but at the cost of yet another function call:
try {
    // ...
} catch (err) {
    ((err: IException) => {
        // ...
    } (err));
Is this something that could be added to TS in the future?

Oct 6, 2013 at 12:39 PM
@FabianLauer, This is a bug and I believe it is fixed in the develop branch.
Oct 6, 2013 at 5:32 PM
@nabog, thanks, didn't find that issue.