Bug reportNov 23, 2026· 0:32· 28 views
TypeScript 6.0.3: control-flow narrowing does not apply inside unreachable code — if (h) leaves 'h' as string | undefined
0:32 total
Comments · 0
Be the first to comment.
Bug reportby SceneRecap AI
lowTypeScript checker: control-flow analysis and narrowing for unreachable code after a return statement (error 7027 path)
os: macOSapp: TypeScript Playground (/play)dpr: 1locale: en-USbrowser: Chromium 151.0.7922.34timezone: Europe/Berlinviewport: 1280x720typescript_version: v6.0.3
Repro steps
- 1.Open the TypeScript Playground at /play with version v6.0.3 selected.
- 2.Click 'Editor content', press Cmd+a, then Cmd+v to paste: function fn() { return; const h = Math.random() > 0.5 ? 'h' : undefined; if (h) { const a: string = h; } }
- 3.Observe the Errors tab badge shows 2.
- 4.Click the 'Errors' tab and read the listed errors.
- 5.Control: click 'Editor content (control)', press Cmd+a, then Cmd+v to paste the same code without 'return;'.
- 6.Observe the narrowing error is gone.
Expected
Inside if (h), 'h' should narrow to string, so const a: string = h is accepted. Only 'Unreachable code detected.' and the unused 'a' notice should appear for code after the early return.
Actual
After the early 'return;', the code is flagged as unreachable. The checker also reports "Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'." inside if (h). Narrowing is not applied in the unreachable block.
Evidence
visualAfter pasting the repro, the code after 'return;' is faded with red squiggles and the Errors tab badge shows 2.
visualErrors panel lists 'Unreachable code detected.', then "Type 'string | undefined' is not assignable to type 'string'.", and 'a' is declared but its value is never read.
consoleWarn: Error panel evidence found text "Unreachable code detected.Type 'string | undefined' is not assignable to type 'string'."
consoleWarn and error: 'Invoking deltaDecorations recursively could lead to leaking decorations.' This is a Monaco editor message and probably not related to the narrowing issue.
consoleThe same 'Invoking deltaDecorations recursively could lead to leaking decorations.' error repeats.
visualHover tooltip on line 4 shows 'Unreachable code detected. (7027)' with 'View Problem' and 'Quick Fix...'.
visualControl case, works correctly: without 'return;' the 7-line code shows no badge and no squiggles. The Errors panel only lists 'a' is declared but its value is never read.
visualOverlay label reads: Control: without 'return;', if (h) narrows correctly, no error.
Issue markdown (copy-paste ready)
## Summary
In TypeScript v6.0.3 (Playground), `if (h)` does not narrow `h` when the code sits after an early `return;`. The checker reports a type error in addition to the unreachable code diagnostic.
## Environment
- TypeScript Playground `/play`, version v6.0.3
- Chromium 151.0.7922.34, macOS, 1280x720
## Repro
```ts
function fn() {
return;
const h = Math.random() > 0.5 ? 'h' : undefined;
if (h) {
const a: string = h;
}
}
```
1. Open the Playground with v6.0.3 (0s).
2. Replace the editor content with the code above (3.4s to 4.1s).
3. Open the Errors tab (9.2s).
## Expected
`h` narrows to `string` inside `if (h)`. Only `Unreachable code detected.` and the unused `a` notice are reported.
## Actual
The Errors badge shows 2 (5s). The panel lists (11s):
- `Unreachable code detected.`
- `Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.`
- `'a' is declared but its value is never read.`
The hover on line 4 shows `Unreachable code detected. (7027)` (17s).
## Control (works)
The same code without `return;` (21s to 23s) narrows correctly. Only the unused `a` notice remains and no badge is shown (23s, 27s).
## Console
- 11.9s: error panel evidence text includes the `string | undefined` error.
- 12.3s and 15.1s: `Invoking deltaDecorations recursively could lead to leaking decorations.` This is a Monaco editor message and is likely unrelated.
## Suggested area
Checker control-flow narrowing in unreachable code.
*Recorded with SceneRecap.*This report was written by SceneRecap from a screen recording. Try SceneRecap free
Turn a screen recording into a bug report like this one.
Repro steps, expected vs actual and the console and network evidence, each with a timestamp. On every plan, Free included.
Try SceneRecap free →