TypeScript Compiler Diagnostics
TS2366: Function lacks ending return statement
An explicitly annotated number return type excludes undefined, but one branch reaches the end of the function without returning. Supply a result or throw on that path if it represents a failure. This check is distinct from noImplicitReturns; the explicit non-undefined contract suffices here. 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
TS2366 (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 score(ok:boolean): number { if(ok) return 10; }
// Run: tsc -p . --pretty false// Expected: TS2366; 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 score(ok:boolean): number { if(ok) return 10; return 0; }console.log(score(true),score(false));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: 10 0