Fix Common TypeScript Compiler Errors

Learn how to read, understand, and fix the most common TypeScript compiler errors. From 'is not assignable' to 'cannot find module', decode what tsc is telling you.

5 min read

TypeScript compiler errors can look intimidating at first. A wall of red text with long type names and line references. But every error follows a consistent pattern: it tells you what went wrong, where, and often what the expected and actual types are.

This article covers the five most common error categories, what they mean, and how to fix TypeScript errors of each kind. You will see these errors frequently, so learning to read them quickly pays off every day.

"Type X is not assignable to type Y"

This is the most common error in TypeScript. It means you are trying to use a value where a different type is expected.

typescripttypescript
function greet(name: string) {
  return `Hello, ${name}`;
}
 
greet(42);

The compiler tells you exactly which type is wrong and what it expected instead. The error message names both the source type and the target type so you can compare them directly:

texttext
Argument of type 'number' is not assignable to parameter of type 'string'.

The fix depends on intent. If the value is wrong, pass the correct type. If the function should accept multiple types, change the parameter type to a union. For cases where you are certain of the runtime type and TypeScript cannot know, a type assertion works, but use it sparingly.

"Object is possibly undefined"

This error appears when strictNullChecks is enabled and you try to use a value that might be missing. It is most common with array methods like .find() and Map.get():

typescripttypescript
const users = [{ name: "Alice" }, { name: "Bob" }];
const user = users.find((u) => u.name === "Charlie");
console.log(user.name);

The compiler knows that .find() can return undefined when no element matches the predicate. It warns you before you dereference a potentially missing value. Here is the exact error it produces:

texttext
'user' is possibly 'undefined'.

The fix is to check for undefined before using the value. Use an if statement, optional chaining, or nullish coalescing. For a deep dive on this topic, see strictNullChecks in TypeScript.

"Cannot find module X or its corresponding type declarations"

This error means TypeScript cannot resolve an import path. Here is what it looks like for a package with no type declarations:

texttext
Cannot find module 'left-pad' or its corresponding type declarations.

It happens for three common reasons: a wrong import path, a package with no type declarations, or a .js import without allowJs enabled.

First, the path is wrong. Check that the relative path or package name is spelled correctly.

Second, the package you imported has no type declarations. Many npm packages do not ship their own types. The fix is to install community-maintained types from the DefinitelyTyped project. This adds type declarations for the package without changing the package itself:

bashbash
npm install --save-dev @types/package-name

If no types exist on DefinitelyTyped, create a small declaration file anywhere in your project so TypeScript stops treating the import as an error:

typescripttypescript
declare module "left-pad";

This is a minimal approach that gives you an untyped module whose default export is any. It removes the error without adding real type safety for that package.

Third, you are importing a .js file but have not enabled the allowJs option. Enable it in your tsconfig if you need to import JavaScript files alongside TypeScript.

"Property X does not exist on type Y"

You are accessing a property that the type definition does not include. This often means the type is too narrow or the code has a typo:

typescripttypescript
const element = document.querySelector(".my-class");
element.value = "hello";

The error tells you exactly what is wrong. The general Element type does not have a value property because only specific element types like HTMLInputElement do. The compiler shows which type it checked and which property was missing:

texttext
Property 'value' does not exist on type 'Element'.

Element is the general base type. The .value property only exists on more specific types like HTMLInputElement. The fix is to narrow the type. You can use a type assertion if you know the element type, or use a type guard with instanceof to narrow safely.

"Parameter X implicitly has an any type"

This error from noImplicitAny means TypeScript cannot figure out the type of a parameter and needs you to annotate it:

typescripttypescript
function calculate(x, y) {
  return x + y;
}

With noImplicitAny enabled, tsc checks every parameter independently and reports one error for each one it cannot infer a usable type for:

texttext
Parameter 'x' implicitly has an any type.
Parameter 'y' implicitly has an any type.

The fix is to add type annotations to every untyped parameter, matching the values the function actually receives. This is covered in detail in noImplicitAny in TypeScript.

How to read a TypeScript error effectively

Every error has the same structure. First, the file path and line number tell you where. Then the error category tells you what kind of problem it is. Finally, the detailed message shows the relevant types.

When you see "Type A is not assignable to type B," look at both A and B. The difference between them is the problem. When you see "Property X does not exist," look at the type Y. It tells you what TypeScript thinks the value is, which is often narrower than what you expected.

The most common fixes across all error types are adding a type annotation, adding a null check, installing type declarations, or narrowing a union type before accessing specific properties. Once you learn these four patterns, you can resolve most errors in seconds.

Rune AI

Rune AI

Key Insights

  • 'Not assignable' means a value does not match the expected type; check the two types in the error.
  • 'Possibly undefined' means strictNullChecks caught a missing value; add a null check.
  • 'Cannot find module' means TypeScript cannot resolve an import; install types or declare the module.
  • 'Property does not exist' means the type does not have that property; check the type or use a type guard.
  • Tip: Read the full error message including both types shown; the answer is usually in the comparison.
RunePowered by Rune AI

Frequently Asked Questions

Why does tsc still emit JavaScript when there are errors?

By default, TypeScript emits output even when type errors exist. This is intentional so errors do not block you from running your code during development. To block emit on errors, set noEmitOnError to true in your tsconfig.

How do I suppress a TypeScript error I know is safe?

Use // @ts-ignore on the line above, or // @ts-expect-error if you want the compiler to warn you when the error is fixed and the comment becomes unnecessary. Use these sparingly. A type assertion (as Type) is often the better fix.

What does 'cannot find module' mean for a package I installed?

The package likely does not include type declarations. Try running npm install --save-dev @types/package-name. If no types exist on DefinitelyTyped, create a .d.ts file with declare module 'package-name'; to tell TypeScript the module exists.

Conclusion

TypeScript compiler errors are detailed and specific for a reason. Each one tells you exactly what is wrong and where. Learning to read them quickly means you spend less time debugging and more time building. The fix is usually a type annotation, a null check, a type assertion, or a configuration tweak.