microsoft / microsoft/TypeScript

allowJs option with namespaces

Open
#9,474 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: JavaScript Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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'."

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two example files and the provided tsconfig.json using TypeScript 1.8.10, then reproduce the TS2300 duplicate-identifier error with allowJs, namespaces, and outFile. Done means the TypeScript and JavaScript namespace code can be bundled into one JavaScript file without modifying the JavaScript or reporting the duplicate identifier.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.