TypeScript Compiler Diagnostics

TS2355: Non-void function must return a value

A function with an explicit number return type has no return statement or non-returning control flow. Logging a value is not returning it to the caller. Return the computed number; use void only for an intentionally effect-only function. Baseline: TypeScript 5.8.3 with the shown configuration. Reproduce in one directory and create the corrected project in another. The diagnostic can have other causes; inspect its location and message before choosing a correction. Both projects keep strict checking enabled.

Formal Syntax & Structure

typescript syntaxtypescript
TS2355 (compiler diagnostic code, not TypeScript syntax)

Working Example

Example Snippettypescript
// INTENTIONALLY INVALID — reproduce// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)function total(a:number,b:number): number { console.log(a+b); }
// Run: tsc -p . --pretty false// Expected: TS2355; no output emitted.
// CORRECTED PROJECT — separate directory// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)function total(a:number,b:number): number { return a+b; }console.log(total(2,5));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: 7
TypeScript Reference