microsoft / microsoft/TypeScript

Differentiate between implicit any and explicit any in the Compiler API.

Aperta
#27,931 1 commento 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

  • "Differentiate between implicit any and explicit any in the Compiler API."

Suggestion

Working with the compiler API I've noticed the type checker doesn't differentiate between any originating from a decision by the author and a default case by the compiler.

In an internal branch I've fixed this by adding a new field to ts.TypeFlags and a new intrinsic type which is any with the flag added. I'm happy to upstream my implementation if this is accepted.

Use Cases

function hello(a, b) {}

function world(a: any, b: any) {
  const c = b;
}

In the current version of TypeScript these 2 methods take identical arguments and can't be differentiated by the signatures alone. Adding explicit any to the compiler allows tools to distinguish these.

I've found this modification particularly valuable when utilizing the type checker for tools that analyse the output of the compiler. An example I've been using this for internaly is calculating the explicit type coverage of TypeScript code which could otherwise not be done.

Examples

    if (ts.isIdentifier(node)) {
      const type = this.checker.getTypeAtLocation(node);
      if (type.getFlags() & ts.TypeFlags.Any &&
           !(type.getFlags() & ts.TypeFlags.Explicit)) {
        this._untypedIdentifiers += 1;
      } else {
        this._typedIdentifiers += 1;
      }
    }

With the compiler change it's now possible to exclude explicit any from type coverage calculations.

Checklist

My suggestion meets these guidelines:

  • 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. new expression-level syntax)

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.

Direzione di ricerca

Inizia esaminando il type checker di Compiler API, ts.TypeFlags e getTypeAtLocation, quindi confronta gli esempi di implicit-any ed explicit-any dell'issue. Il lavoro è completo quando l'API può distinguere un any scritto dall'autore da un any predefinito dal compilatore senza modificare il JavaScript emesso, e quando il flag proposto e il comportamento del tipo intrinseco sono documentati e testati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.