Technology · TypeScript
TypeScript Error Messages
Understand common TypeScript errors and how to fix them.
TL;DR
- 01Type mismatch errors indicate your value doesn't match the declared type.
- 02Cannot find name errors mean the variable doesn't exist or isn't imported.
- 03Property doesn't exist errors indicate misspelled properties or missing types.
Type Mismatch Errors
- Type is not assignable to parameter type.
function greet(name: string) { console.log(`Hello ${name}`); } greet(123); // error: number is not assignable to string // Fix: greet("Alice"); - Property is missing from type.
interface User { name: string; age: number; } const user: User = { name: "Alice" }; // error: property 'age' is missing // Fix: add age property - Property value has wrong type.
interface User { age: number; } const user: User = { age: "30" }; // error: string is not assignable to number // Fix: change age to 30 (without quotes) - Union type is too broad for the required narrow type.
function setStatus(status: "active" | "inactive") { } const s: string = "active"; setStatus(s); // error: string is not assignable // Fix: type s as the exact union or use a literal setStatus(s as "active" | "inactive"); - Return type does not match function's declared type.
function getId(): number { return "abc"; // error: string is not assignable to number } // Fix: return a number function getId(): number { return 42; }
Cannot Find Name Errors
- Variable is not defined in scope.
console.log(userName); // error: Cannot find name 'userName' // Fix: declare the variable first const userName = "Alice"; console.log(userName); - Import is missing or misspelled.
const express = require("express"); app.listen(); // error: cannot find name 'app' // Fix: create app instance const app = express(); - Variable is out of scope due to block scoping.
if (true) { const x = 5; } console.log(x); // error: x is out of scope // Fix: move console.log inside the if block or use let/var - Type or interface is used but never imported.
const user: User = { name: "Alice" }; // error: Cannot find name 'User' // Fix: add the import import { User } from "./types"; - Global type not available because lib config is missing.
// error: Cannot find name 'Promise' // Fix: add "es2015" or "es6" to lib in tsconfig.json // { "compilerOptions": { "lib": ["es6", "dom"] } } const p: Promise<void> = Promise.resolve();
Property Does Not Exist Errors
- Property is misspelled.
interface User { name: string; } const user: User = { name: "Alice" }; console.log(user.Name); // error: Property 'Name' does not exist // Fix: use lowercase 'name' - Property exists but type is not declared in the interface.
const user = { name: "Alice" }; console.log(user.age); // error: property 'age' does not exist // Fix: add age to the type interface User { name: string; age: number; } - Object might be null or undefined before access.
const user: User | null = null; console.log(user.name); // error: object is possibly null // Fix: add null check if (user) { console.log(user.name); } - Accessing a property on a union type not shared by all members.
type Shape = { kind: "circle"; radius: number } | { kind: "square"; side: number }; function area(s: Shape) { return s.radius; // error: 'radius' does not exist on 'square' // Fix: narrow with a type guard if (s.kind === "circle") return Math.PI * s.radius ** 2; } - Dynamic property key not present in the index signature.
const map: Record<string, number> = {}; const val: number = map["key"]; // may be undefined // Fix: check first or use optional chaining const val2 = map["key"] ?? 0;
Function Errors
- Function arguments don't match parameters.
function add(a: number, b: number): number { return a + b; } add(1); // error: 1 argument provided but 2 required // Fix: provide both arguments add(1, 2); - Return type doesn't match function signature.
function getName(): string { return 123; // error: number is not assignable to string } // Fix: return a string function getName(): string { return "Alice"; } - Value is not callable because it is not a function.
const x = 5; x(); // error: This expression is not callable // Fix: only call variables that are functions - Async function return type is missing the Promise wrapper.
async function loadUser(): User { // error: missing Promise return fetchUser(1); } // Fix: wrap return type in Promise async function loadUser(): Promise<User> { return fetchUser(1); } - Too many arguments passed to a function.
function greet(name: string) { } greet("Alice", "extra"); // error: Expected 1 arguments, but got 2 // Fix: remove extra argument or add a parameter
Fixing with Type Assertions
- Use as to tell TypeScript the actual type.
const value: unknown = "hello"; const length = (value as string).length; // OK - Use type guards to narrow types safely without assertions.
const value: string | number = "hello"; if (typeof value === "string") { console.log(value.length); // OK — TypeScript knows it's a string } - Use non-null assertion (!) only when you are certain the value exists.
const user: User | null = getUser(); console.log(user!.name); // tells TypeScript user is not null // Only use if you're certain the value is not null - Use satisfies to validate a value without widening its type.
const config = { host: "localhost", port: 3000 } satisfies Record<string, string | number>; // config.port is still typed as number, not string | number - Cast through unknown when TypeScript won't accept a direct assertion.
const value = getAnything() as unknown as SpecificType; // Use sparingly — only when you know the runtime type is correct
Tip: Read the full error message carefully — TypeScript provides helpful suggestions for how to fix the error.
Warning: Avoid using
anyoras anyto silence errors — fix the underlying type issues instead for safer code.