You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to see TypeScript complain about (most) conditionals that can never possibly be true. Example:
constx=[1];if(xinstanceofString){// Should complain here...console.log('unreachable')}
This is a contrived toy example; see below for a motivating use case.
Given that this increases the range of conditions under which errors may be newly emitted, I don't know whether this would need to be gated behind a compiler option that people desiring more strictness could opt-in to.
Use Cases
In real world code, a common mistake is to forget to await an async function:
// This function does something async, like making a network request.declarefunctionfoo(): Promise<Date|null>;asyncfunctioncaller(){// BUG: missing await keywordconstval=foo();if(valinstanceofDate){console.log('unreachable, because we forgot await')}}
Examples
Ideally, TS would complain that the if (val instanceof Date) conditional is impossible and that the console.log is therefore unreachable and very likely indicative of a programming error.
Counterexamples which are not likely to be indicative of programming errors are things like then following, which are most likely to intentional/temporary deactivations of code:
I think this is probably a duplicate of #32801 + #30646.
TypeScript already produces errors for some set of conditions that are always known to be false. The proposal in #32801 would add your example to the list of errors produced. Once you have an error, it might be reasonable to expect a quickfix that suggests inserting await to make the condition non-trivial.
Search Terms
Possibly duplicates #34955 (but I am not sure).
Suggestion
I'd like to see TypeScript complain about (most) conditionals that can never possibly be true. Example:
This is a contrived toy example; see below for a motivating use case.
Given that this increases the range of conditions under which errors may be newly emitted, I don't know whether this would need to be gated behind a compiler option that people desiring more strictness could opt-in to.
Use Cases
In real world code, a common mistake is to forget to
await
anasync
function:Examples
Ideally, TS would complain that the
if (val instanceof Date)
conditional is impossible and that theconsole.log
is therefore unreachable and very likely indicative of a programming error.Counterexamples which are not likely to be indicative of programming errors are things like then following, which are most likely to intentional/temporary deactivations of code:
I'd suggest that for these and perhaps a small range of other cases TS should refrain from complaining.
Checklist
Suggestion meets these guidelines? Not sure, but I think I can tick off at least a couple of them:
The text was updated successfully, but these errors were encountered: