microsoft / microsoft/TypeScript

Allow type annotations in .js files in preparation for Type Annotations proposal

Offen
#59,648 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔍 Search Terms

allow js file extension

✅ Viability Checklist
⭐ Suggestion

Add an option to allow removing the error "[some feature] can only be used in TypeScript files"

Previous discussion: #10939

The compiler option would allow consuming .js files as if they were .ts files, but with some differences (see below). All type-checking, type-stripping, and code emitting features would remain as-is.

📃 Motivating Example

The Type Annotations proposal is the definite future of JavaScript.

This feature would:

  • Enable incremental adoption of the Type Annotations proposal by users
  • Enable incremental support of Type Annotations in the TypeScript compiler
  • Possibly help push that proposal forward a little more quickly by increased usage
  • Spread word that this proposal exists by gaining compiler support in tsc and its release notes

It's true that the exact syntax isn't definite yet, and it will probably be at least somewhat different than current TypeScript syntax.

However, that's exactly why adding initial support for plain TypeScript would not interfere with its eventually sealed syntax.

In other words, the initial support would simply parse it as "typed-javascript" but the TypedJavaScript parser would currently be exactly the same as the TypeScript parser for the time being, and allow gradual differentiation.

It would also be a good opportunity to allow TypedJavaScript mode to disallow namespaces, enums, constructor fields, etc.

💻 Use Cases
  1. What do you want to use this for?

Writing future-ready JavaScript.

  1. What shortcomings exist with current approaches?

It doesn't take into account the pending type annotation proposal.

  1. What workarounds are you using in the meantime?

I can almost get this working with some VS Code hacks, but then tsc complains as soon as I run it.

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 damit, die vorherige Diskussion in #10939 und den verlinkten TC39 Type Annotations-Vorschlag zu lesen. Untersuche dann die bestehenden Einschränkungen des TypeScript-Compilers für TypeScript-only-Syntax in .js-Dateien und bestimme den Umfang der Option, einschließlich der Konstrukte, die weiterhin nicht zulässig sind. Als erledigt gilt die Aufgabe, wenn eine dokumentierte Compiler-Option das beabsichtigte Parsen von JavaScript mit Typannotationen unterstützt, ohne das ausgegebene Laufzeitverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, 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.