Table of contents
- 1. Error TS1196
- 2. Type Guards
- 3. Type Predicate
- 4. Video Tutorial
By default, TypeScript’s compiler doesn’t allow you to add a custom type annotation to an error in a try-catch statement (TS1196). That’s because the underlying code can throw any type of error, even system generated exceptions, which makes it impossible for TypeScript to know the error type from the start. Luckily, there is the concept called “type guards“ which can help the TypeScript compiler to infer a specific type.
TypeScript’s compiler will throw an error when you assign a custom type to an error in a catch block:
TS1196: Catch clause variable type annotation must be ‘any’ or ‘unknown’ if specified.
To circumvent the type annotation error, you can use a type guard.
A type guard is a runtime check which guarantees a type in a defined scope. There are different forms of type guards. The simplest type guard is a conditional block paired with an
A more complex type guard is a function which returns a type predicate. A type predicate is identified by the
is keyword. The following code shows type-safe error handling in action:
The following video shows how to use a type guard with an error object. This tip can be very useful when you want to work with the properties of an error: