Jatt
Jatt

Reputation: 785

Cannot re declare block scoped variable error when I am not re-declaring it anywhere in my file

I get an error that says error TS2451: Cannot redeclare block-scoped variable 'packageList' in a file named package-list.ts that has the following code:

const packageList = {
    'FREE': 'free',
    'GOLD': 'gold',
    'SILVER': 'silver',
    'BRONZE': 'bronze',
    'STARTER': 'starter'
}

module.exports = packageList;

with the tsc compiler options:

    {
    "compilerOptions": {
        "target": "es2015",
        "outDir": "../dist",
        "allowJs": true,
        "module": "commonjs",
        "noEmitOnError": false,
        "noImplicitAny": false,
        "strictNullChecks": true
    },
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

I have not redeclared packageList anywhere in the above code. What could then be the reason for this error?

Upvotes: 0

Views: 64

Answers (1)

Anshuman Jaiswal
Anshuman Jaiswal

Reputation: 5462

It's because packageList is being used by some other module. You can try following options:

Solution 1

Change the name packageList to myPackageList.

Solution 2.

Don't include DOM typings. To do this, add an explicit lib property in your tsconfig.json that does not include dom:

{
    "compilerOptions": {
        "lib": [
            "es2015"
        ]
    }
}

Solution 3.

Try by adding export {}; at top.

Upvotes: 1

Related Questions