microsoft / microsoft/TypeScript

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

Offen
#27,931 1 Kommentar 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

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)

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.

Rechercherichtung

Beginne mit der Überprüfung des Typprüfers der Compiler API, von ts.TypeFlags und getTypeAtLocation und vergleiche anschließend die Beispiele für implicit-any und explicit-any des Issues. Die Aufgabe ist abgeschlossen, wenn die API vom Autor geschriebenes any von compiler-default any unterscheiden kann, ohne das emittierte JavaScript zu ändern, und wenn das vorgeschlagene Flag sowie das Verhalten des intrinsischen Typs dokumentiert und getestet sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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