question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generalize `never` type handling for control flow analysis based type guard

See original GitHub issue

I 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:open
  • Created 7 years ago
  • Reactions:29
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
simonbuchancommented, Mar 12, 2019

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.

1reaction
masaeeducommented, Nov 18, 2017

I currently use const guaranteedFoo = foo || throwError() to strip away undefined from stuff that I know should never be undefined.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found