TS2345 🟩 Node.js

TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

A value’s type doesn’t match the function parameter — commonly a possibly-undefined value passed where a definite one is required.

Seen on: Node.js

Meaning

strictNullChecks flags values that may be undefined (env vars, optional props, find() results). Narrow or provide defaults rather than casting.

Common causes

  • process.env values (string | undefined)
  • Optional properties
  • Array.find() results
  • Mismatched object shapes

⚡ Quick fix

  1. Narrow with if checks or ?? defaults
  2. Validate env vars at startup
  3. Use the non-null assertion ! only when you’re certain

Detailed fix by platform

Node.js

  1. const port = Number(process.env.PORT ?? 3000);

How to diagnose

  1. Types — Expected vs actual type
  2. Source — Why can it be undefined?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.