Generalize `never` type handling for control flow analysis based type guard
See original GitHub issueI guess there would be some duplicates, but I cannot find it.
TypeScript Version: 2.1.14
Code
function throwError(): never {
throw new Error();
}
let foo: string | undefined;
if (!foo) {
throwError();
}
foo; // Expected to be `string` instead of `string | undefined`.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:29
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Type Guards and Control Flow Analysis in TypeScript 4.4
This type programming capability allows us to cut down on code duplication and have a single source of truth when defining types. In...
Read more >Get the best of TypeScript Control Flow Analysis - Retool
Control Flow Analysis is a core TypeScript feature that analyzes the code to get the best type inference depending on variable usages; ...
Read more >LLVM Language Reference Manual
This document is a reference manual for the LLVM assembly language. LLVM is a Static Single Assignment (SSA) based representation that provides type...
Read more >On the Effectiveness of Type-based Control Flow Integrity - arXiv
Even fine-grained points-to analysis-based. CFI techniques are shown to be too permissive to prevent all forms of control hijacking attacks, either because of ......
Read more >property 'contains' does not exist on type 'never'. - You.com
Control flow analysis of aliased conditional expressions // (e.g. type guard result saved in a variable) are introduced // only in v4.4.0 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For other people googling this, this issue was addressed in #14490. Basically it seems the issue is TS currently has separate passes for flow control and type assignment, and the former needs to run first for the latter to work, and there didn’t seem to be any clean solutions that would also handle imported functions, etc… So the current workaround is just
return neverReturningFunction();
, which won’t alter the return type and lets flow control do its thing.I currently use
const guaranteedFoo = foo || throwError()
to strip away undefined from stuff that I know should never beundefined
.