microsoft / microsoft/TypeScript

Identities should report an error

Aperta
#55,699 6 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@DanielRosenwasser ci sta già lavorando.

Dal 12/9/2023.

Experimentation Needed Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.