Sub topic 11
Unknown Catch Variables and `alwaysStrict`
This chapter assumes you know basic TypeScript functions, type annotations, tsconfig.json, and how to run tsc. It focuses on two compiler options: useUnknownInCatchVariables and alwaysStrict.
`useUnknownInCatchVariables`
JavaScript allows code to throw any value:
throw new Error("Request failed");
throw "network failure";
throw { code: 503 };Because the thrown value can have any shape, TypeScript treats a caught value as unknown when strict is enabled. The unknown type means “a value exists, but its type has not been verified.”
You must narrow the value before reading properties or calling methods.
function parseSettings(text: string): object {
try {
return JSON.parse(text) as object;
} catch (error) {
if (error instanceof Error) {
console.error(`Invalid settings: ${error.message}`);
} else {
console.error("Parsing failed with a non-Error value");
}
return {};
}
}The instanceof Error check narrows error from unknown to Error. TypeScript then allows access to error.message.
You can also narrow primitive values with typeof, or check the shape of an object before using it.
try {
doWork();
} catch (error) {
if (typeof error === "string") {
console.error(error.toUpperCase());
} else if (error instanceof Error) {
console.error(error.message);
}
}Avoid this shortcut:
catch (error) {
console.error((error as Error).message);
}A type assertion changes what TypeScript believes, but it does not check the value at runtime. If another part of the program throws a string, .message may be undefined.
If a particular catch block intentionally accepts any value, you can opt out locally with an explicit any annotation where supported by your TypeScript version. Prefer unknown when possible because it preserves the safety check.
`alwaysStrict`
The alwaysStrict option has two effects:
- TypeScript parses every source file using JavaScript strict-mode rules.
- For scripts that are emitted as non-module JavaScript, TypeScript adds
"use strict".
Enable it in tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"alwaysStrict": true
}
}Strict mode changes runtime behavior. For example, assigning to an undeclared variable throws an error instead of silently creating a global variable. It also makes a plain function’s this value become undefined when the function is called without an owner.
ES modules are strict by definition, so files that remain ES modules already receive strict runtime behavior. alwaysStrict still matters for script files and makes the intent explicit across a project.
Checking the result
Run the compiler without emitting JavaScript:
npx tsc --noEmitIf code reads error.message without narrowing error, TypeScript reports an error under useUnknownInCatchVariables. If you want to inspect whether alwaysStrict is active, run:
npx tsc --showConfigLook for both options in the resolved compilerOptions object.
Exercise
Why does TypeScript require a check before error.message when strict is enabled?
Show answer
A catch variable has type unknown, because JavaScript can throw values of any type. The value must be narrowed to Error or another known shape first.