microsoft / microsoft/TypeScript

allowJs option with namespaces

Offen
#9,474 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: JavaScript Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

I am currently trying to put the compiler option "allowJs" to use. My goal is to convert an existing JavaScript application into TypeScript step by step. The application does not use (external) modules, so I'm converting the JavaScript namespaces into real TypeScript namespaces, but I'm running into "duplicate identifier" errors as outlined below. Shouldn't it be possible to combine TypeScript and JavaScript using the same namespace? Maybe I'm doing something wrong or this is not intended, but I expected this to work.

TypeScript Version: 1.8.10

Code
File 1:

namespace myapp {
    export var c1 = 5;
}

File 2:

var myapp;
(function (myapp) {
    myapp.c2 = 4;
})(myapp || (myapp = {}));

tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "outFile": "dist/appBundle.js"
    }
}

Expected behavior:
Bundling the compiled code of file 1 and the code of file 2 without modifications into a single JavaScript file.

Actual behavior:
The compilation is done, but the compiler throws an error: "error TS2300: Duplicate identifier 'myapp'."

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 den beiden Beispieldateien und der bereitgestellten tsconfig.json unter Verwendung von TypeScript 1.8.10 und reproduziere dann den TS2300-Fehler wegen doppelter Bezeichner mit allowJs, namespaces und outFile. Als erledigt gilt, wenn der TypeScript- und der JavaScript-Code für namespaces ohne Änderung des JavaScript-Codes oder Meldung des doppelten Bezeichners in eine einzige JavaScript-Datei gebündelt werden kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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