TypeScript Compiler Diagnostics

TS7030: Not all code paths return a value

With noImplicitReturns enabled, a function that returns a value on only some paths is flagged even when its inferred return type could include undefined. Make the intended fallback explicit. This example keeps the option enabled and returns a number on both paths. 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
TS7030 (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",    "noImplicitReturns": true  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)function score(ok:boolean) { if(ok) return 10; }
// Run: tsc -p . --pretty false// Expected: TS7030; 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",    "noImplicitReturns": true  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)function score(ok:boolean) { 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
TypeScript Reference