Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible conditionals should be errors #37804

Closed
4 of 5 tasks
wincent opened this issue Apr 6, 2020 · 2 comments
Closed
4 of 5 tasks

Impossible conditionals should be errors #37804

wincent opened this issue Apr 6, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@wincent
Copy link
Contributor

wincent commented Apr 6, 2020

Search Terms

  • "unreachable"

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:

const x = [1];

if (x instanceof String) {
    // 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.
declare function foo(): Promise<Date | null>;

async function caller() {
    // BUG: missing await keyword
    const val = foo();

    if (val instanceof Date) {
        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:

if (false) {
  doSomething();
}

if (0) {
  doSomethingElse();
}

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 11, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants