microsoft / microsoft/TypeScript

Identities should report an error

Offen
#55,699 6 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@DanielRosenwasser arbeitet bereits daran.

Seit 12.9.2023.

Experimentation Needed Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms

Identity TS2367 impossible comparison

✅ Viability Checklist
  • 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

Comparing identical literal (abc, 5, false) values in an "if" it should report it as redundant/impossible, similar to what TS2367 does.

📃 Motivating Example

https://www.typescriptlang.org/play?#code/FAMwrgdgxgLglgewgAhAhAKAlALlQQwBsBnAU2QG9hkbkAnUmMOlEIsgbmAF9hRJYiFACN8dbHgDk+SZWq0GTFskmSuvftHhJkMAIwYxAczyQA1hAQB3CFjm1kcEMgyp02ZAEIAvN910wcjsqeRoNBycXN0w7Xz8YAKC5UORw2kjXNBivXwISJJCHNJoM6I84vLJkYL4i2vTnV1FxOx8-SWFZGpTix0bkZvLcjq7kupTSwdbhmWqx2l7JsSH22e663iA


function foo(): false {
    return false;
}

function bar(): 'a' {
    return '';
}

function t1(arg: unknown) {
    if ( foo() !== true ) {

    }

    if ( foo() === true ) {

    }

    if ( foo() !== false ) {

    }

    if ( foo() === false ) {

    }

    if ( bar() !== 'b' ) {

    }

    if ( bar() === 'b' ) {

    }

    if ( bar() !== 'a' ) {

    }

    if ( bar() === 'a' ) {

    }
}
💻 Use Cases
  1. What do you want to use this for?
    Get rid of legacy conditions/impossible code

  2. What shortcomings exist with current approaches?
    No error reported

  3. What workarounds are you using in the meantime?
    None, wasting time manually checking legacy code to see what can be removed.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.