Read TypeScript Compiler Errors
TypeScript error messages are precise and structured. Learn to read the error format, understand the most common error codes, and fix each one quickly.
TypeScript compiler errors follow a consistent format. Once you know how to read them, fixing them becomes fast. Every error tells you three things: where the problem is, what the problem is, and what TypeScript expected instead.
Here is a typical error:
app.ts:5:10 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.Breaking it down:
| Part | Meaning |
|---|---|
app.ts | The file with the error |
:5 | Line number |
:10 | Column (character position on that line) |
TS2345 | Error code (unique identifier) |
| The sentence after the code | Plain-English description of the problem |
The error code is useful for searching, but the sentence usually tells you everything you need. Let's look at the errors you will see most often.
The Error Format in Detail
When you compile a file with tsc, the terminal shows the exact error together with the surrounding source code, not just a bare message. Here is a small file with a type mistake:
// app.ts
function double(n: number): number {
return n * 2;
}
const result = double("hello");Save the file, then compile it from the terminal exactly the way you normally would when running any TypeScript file:
tsc app.tsThe compiler prints the file, the exact position, the error code, and a snippet of the offending line with a marker under the problem:
app.ts:5:22 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
5 const result = double("hello");
~~~~~~~The compiler shows the exact line with a squiggly underline under the problematic part. The underline points to "hello" because that is the argument with the wrong type. This visual marker makes it easy to spot the issue even in longer lines.
Most Common Error Codes
Here are the errors you will encounter most often as a beginner.
TS2345: Wrong Argument Type
You passed a value whose type does not match the parameter type.
function printLength(text: string) {
console.log(text.length);
}
printLength(42);Compiling this reports the mismatch immediately, pointing at the call that passed the wrong type, instead of letting it reach runtime:
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.Fix: pass a string, or convert the number to a string with String(42) or 42.toString().
TS2339: Property Does Not Exist
You tried to access a property that the type does not have.
const user = { name: "Alex", age: 28 };
console.log(user.email);TypeScript checks the object's shape and reports exactly which property is missing, right down to a spelling suggestion when one is available:
error TS2339: Property 'email' does not exist on type '{ name: string; age: number; }'.Fix: check your spelling, or add the property to the type. The error often suggests the correct name if you made a typo.
TS18047 / TS2531: Possibly Null or Undefined
You tried to use a value that might be null or undefined without checking first.
function getLength(text: string | null) {
return text.length;
}The compiler will not let this compile until you prove the value cannot be null, and reports exactly that at the line in question:
error TS18047: 'text' is possibly 'null'.Fix: add a null check before using the value, so the compiler can prove it is safe at that point in the code:
function getLength(text: string | null) {
if (text === null) return 0;
return text.length; // Safe now
}TS2554: Wrong Number of Arguments
You called a function with too few or too many arguments. TypeScript knows the exact parameter list for every function, so it checks the call site against it every time.
function multiply(a: number, b: number) {
return a * b;
}
multiply(5);The compiler counts the arguments at every call site and reports the exact expected and actual counts when they do not match:
error TS2554: Expected 2 arguments, but got 1.Fix: pass the correct number of arguments, or make the parameter optional with b?: number.
TS2322: Wrong Assignment Type
You tried to assign a value to a variable or property of a different type.
let age: number = 25;
age = "twenty-five";Once a variable has an inferred or annotated type, TypeScript enforces it on every later assignment and reports a mismatch immediately:
error TS2322: Type 'string' is not assignable to type 'number'.Fix: assign a number, or change the variable's type annotation.
TS18048: Possibly Undefined in an Object
You accessed a property on something that might be undefined.
type User = { name: string } | undefined;
function getName(user: User) {
return user.name;
}This works the same way as the null check earlier, except the value here is a union that includes undefined instead:
error TS18048: 'user' is possibly 'undefined'.Fix: check that the value exists before accessing its properties.
Fix Errors Top to Bottom
When you have multiple errors, fix the first one. Later errors are often caused by the first one, not by independent problems.
function getUser(id: number) {
return { name: "Alex", id };
}
const user = getUser("abc"); // Error 1: string is not number
console.log(user.name.toUpperCase()); // Error 2: user might be undefined
console.log(user.email); // Error 3: email doesn't existCompiling this file all at once produces three errors, though only the first one is the real root cause to fix:
app.ts:5:25 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
app.ts:6:21 - error TS18048: 'user' is possibly 'undefined'.
app.ts:7:21 - error TS2339: Property 'email' does not exist on type '{ name: string; id: number; }'.Error 2 and 3 are real, but they might disappear after fixing Error 1 if the function return type inference changes. Always fix from the top. After changing "abc" to 1, recompile and see which errors remain.
Errors in the Editor vs the Terminal
In VS Code, TypeScript errors appear as red squiggly lines as you type. These are the same errors tsc would report. The editor shows them faster because the TypeScript language server runs continuously in the background.
If you see errors in VS Code that tsc does not report, check:
- The TypeScript version in VS Code's status bar. It might differ from your global tsc.
- Whether your tsconfig.json is in the project root. VS Code looks for it there.
- Whether you have strict: true in your tsconfig. VS Code uses your config settings.
Errors You Can Safely Ignore (Sometimes)
Not every error means your code is broken. Some are style preferences or strictness choices.
noUnusedLocals (TS6133): a variable is declared but never used. This is a warning that helps you clean up dead code. It does not mean your logic is wrong.
noImplicitAny (TS7006): a parameter has no type annotation and TypeScript cannot infer it. This is a reminder to add types, not a bug. In a quick script, you might leave it. In production code, add the annotation.
These checks are enabled by strict: true. You can disable individual checks in your tsconfig if they get in the way, but keeping them on produces better code.
For a full walkthrough of strict mode options, see TypeScript compiler explained. To learn how to set up a complete project, see TypeScript project setup for beginners.
Rune AI
Key Insights
- Every TypeScript error has a location, a code, and a plain-English description.
- TS2345 means you passed the wrong type to a function. TS2339 means a property does not exist.
- TS18047 means a value might be null and you need to check it first.
- Read the error from top to bottom. The first error is often the real cause of later errors.
- Use
--noEmitOnErrorto prevent JavaScript output when type errors exist.
Frequently Asked Questions
Why does TypeScript show errors but still produce JavaScript output?
What does the TS error code mean?
Why do I get errors in my editor but not when I run tsc?
Conclusion
TypeScript error messages follow a consistent format: file location, error code, and a plain-English description of what is wrong. The most common errors are type mismatches (TS2345), missing properties (TS2339), possibly-null values (TS18047), and missing arguments (TS2554). Read the message, look at the line it points to, and the fix is usually clear. The compiler tells you what it expected and what it got. Trust it.
More in this topic
TypeScript Compiler Explained
The TypeScript compiler (tsc) turns .ts files into .js files while checking types. Learn what it does, how to use it, and how it fits your workflow.
Set Up ESLint for TypeScript
Install and configure ESLint with typescript-eslint to catch bugs and enforce consistent code style in your TypeScript project.
Pick Utility Type in TypeScript
Pick creates a new type by selecting only the properties you name from an existing type. Use it to build focused types for specific views, API responses, or function parameters.