You encountered the error “Catch clause variable type annotation must be any or unknown if specified” while working on a TypeScript project and still don’t know how to fix it. This article will give you the solution. Read on it now.
What does this error mean?
When trying to type a variable containing an error value in the catch block of the try-catch code block, the error message “Catch clause variable type annotation must be any or unknown if specified” will appear.
The reason is that we can’t be sure what type the error variable is. So typing it to a certain type will break the problem.
Reproducing the error:
function getResult(firstNumber: number, secondNumber: number) {
try {
return firstNumber + secondNumber;
} catch (error: Error) {
return error.message;
}
}
Output:
How to fix the error “catch clause variable type annotation must be any or unknown if specified“?
We provide you with two approaches to resolve this error. Let’s see what they are.
Use type guard
To solve this problem, we suggest you use the type guard to slight the scope of the object’s type before we access a particular property.
function getResult(firstNumber: number, secondNumber: number) {
try {
return firstNumber + secondNumber;
} catch (error) {
if(error instanceof Error ){
return error.message;
}else{
return error
}
}
}
In this example, the instanceof operator has the task of checking if ‘error‘ is an instance of the error object.
Once you’ve narrowed down the type inside the if statement, you can safely access the message property without worrying about errors. Otherwise, TypeScript will default the type of ‘error‘ to ‘unknown‘.
Use type assertion
If the above method doesn’t work, you can use type assertion.
Type Assertion allows you to set a variable’s type and tells TypeScript not to infer the variable’s type on its own. Now we can manage the type of variable ourselves.
function getResult(firstNumber: number, secondNumber: number) {
try {
return firstNumber + secondNumber;
} catch (error: Error) {
return error.message;
}
}
With this approach, we have to type the error variable that declares in the catch statement of the try-catch block to a specified type.
As you can see, we use the ‘as‘ keyword to cast ‘error‘ to a variable of type ‘Error‘.
Note: In case you use the Axios library, you can use the function isAxiosError(). IsAxiosError() is used for the same purpose as type guard.
Summary
In conclusion, that’s all we want to cover in this article. We hope you have found a suitable solution to fix the error “Catch clause variable type annotation must be any or unknown if specified” in TypeScript. Good luck for you!
Maybe you are interested:
- This expression is not callable. Type ‘String’ has no call signatures
- This expression is not callable. Type ‘#’ no call signatures in TypeScript
- Property does not exist on type String in TypeScript

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js