Skip to content

Bogus "duplicate identifier" when checking JS code #39918

Open
@wlinna

Description

@wlinna

TypeScript Version: >= 3.9
The problem doesn't occur when compiling with 3.8.* or 3.9.0-beta, but all other versions of 3.9, 4.0 and 4.1 reproduce the issue.

Search Terms: duplicate identifier

Code

a.js

/** @type {import('./types').Config} */
const data = JSON.parse('');
data.uploads = {};


module.exports = data;

types.d.ts

export interface Config {
    uploads: {
    }
}

tsconfig.json

{
    "compilerOptions": {
        "noEmit": true,
        "allowJs": true,
        "checkJs": true
    },
    "include": [ "*.js", "*.ts" ]
}

Run tsc

Expected behavior: Code compiles without errors

Actual behavior:

a.js:3:1 - error TS2300: Duplicate identifier 'uploads'.

 data.uploads = {};
  ~~~~~~~~~~~~

  types.d.ts:2:5
         uploads: {
          ~~~~~~~
    'uploads' was also declared here.


Found 1 error.

Workarounds:
Specifying type of module.exports like the following:
a.js

/** @type {import('./types').Config} */
const data = JSON.parse('');
data.uploads = {};

/** @type {import('./types').Config} */
module.exports = data;

Exporting data differently:
module.exports = { data };

Changing the file extension of a.js to ts (however, this not an option for us, we are only using tsc for type checking)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptcheckJsRelates to checking JavaScript using TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions