TypeScript Error Messages

Diagnose and resolve frequent TypeScript compiler errors including type mismatches, missing names, and missing properties.

TL;DR

  1. Resolve type mismatches by inspecting declared interface or primitive types.
  2. Fix missing name errors by importing identifiers with import statements.
  3. Narrow union objects before accessing properties using the in operator.

Type Mismatch Diagnostics

    Argument type mismatch

    Resolve mismatched function argument types.

    function setAge(age: number): void {
      console.log(age);
    }
    // Error: setAge("25");
    setAge(25);
    Missing interface property

    Provide all required fields in object literals.

    interface User {
      id: string;
      name: string;
    }
    // Error: const u: User = { id: "u1" };
    const u: User = { id: "u1", name: "Ada" };
    Literal union mismatch

    Pass exact permitted union string values.

    type Mode = "read" | "write";
    function openFile(mode: Mode): void {}
    // Error: const m = "read"; openFile(m);
    const m: Mode = "read";
    openFile(m);
    Return type mismatch

    Align return value with function annotation.

    function getStatus(): boolean {
      // Error: return "true";
      return true;
    }

Cannot Find Name Diagnostics

    Missing module import

    Import identifier from its declaring module.

    // Error: Cannot find name 'render'
    import { render } from "./renderer";
    render();
    Block scope visibility

    Reference variables within their declared block.

    let score = 0;
    if (true) {
      score = 100;
    }
    console.log(score);
    Missing global definitions

    Install type definitions for runtime globals.

    // For Node.js globals like process:
    // npm i -D @types/node
    console.log(process.env.NODE_ENV);
    Type import requirement

    Explicitly import referenced type interfaces.

    import type { Config } from "./config";
    const settings: Config = { debug: true };

Property Does Not Exist

    Discriminated union access

    Narrow unions before accessing specific members.

    type Shape =
      | { kind: "circle"; radius: number }
      | { kind: "square"; side: number };
    function getRadius(s: Shape): number {
      if (s.kind === "circle") {
        return s.radius;
      }
      return 0;
    }
    In operator existence check

    Test for presence of property at runtime.

    type Admin = { role: string; perms: string[] };
    type Guest = { role: string };
    function check(user: Admin | Guest): boolean {
      if ("perms" in user) {
        return user.perms.length > 0;
      }
      return false;
    }
    Dynamic dictionary indexing

    Declare index signature for dynamic access.

    interface Dictionary {
      [key: string]: number;
    }
    const scores: Dictionary = {};
    scores["alpha"] = 10;

Function Signature Diagnostics

    Wrong argument count

    Pass required number of positional arguments.

    function multiply(a: number, b: number): number {
      return a * b;
    }
    // Error: multiply(5);
    multiply(5, 10);
    Async promise return

    Annotate async functions with Promise wrapper.

    // Error: async function load(): string {}
    async function load(): Promise<string> {
      return "data";
    }
    Non callable value

    Ensure expressions are functions before call.

    const handler: (() => void) | undefined = undefined;
    // Error: handler();
    handler?.();
    Void function return check

    Prevent returning values from void functions.

    function logMessage(msg: string): void {
      console.log(msg);
      // Avoid: return true;
    }

Type Assertion Solutions

    Safe unknown narrowing

    Narrow unknown types using typeof conditions.

    function processInput(val: unknown): string {
      if (typeof val === "string") {
        return val.trim();
      }
      return String(val);
    }
    Satisfies validation

    Validate structure while preserving literals.

    type Theme = Record<string, string>;
    const theme = {
      primary: "#0070f3",
      secondary: "#ff0080"
    } satisfies Theme;
    console.log(theme.primary.toUpperCase());
    DOM type assertion

    Assert specific HTMLElement subtypes.

    const input = document.getElementById("search") as
      HTMLInputElement | null;
    input?.focus();
    Double assertion escape

    Cast via unknown for incompatible conversions.

    // Use with extreme caution only:
    const raw: string = "42";
    const val = raw as unknown as number;

Tips

  1. Hover over red squigglies in your editor or run tsc --pretty to read full diagnostic message traces with helpful suggestions.
  2. Use the satisfies operator to validate object structures against types without discarding specific inferred literal types or widening values.

Warnings

  1. Avoid using any or as any type assertions to bypass compiler errors, as this destroys type safety throughout your application.
  2. Suppressing diagnostics with @ts-ignore comments hides legitimate breaking changes during future dependency upgrades and compiler refactors.

In Practice

FAQ