Skip to content

TS >=4.1: Cannot assign because it is not a variable (require does not work with let anymore) #41628

Closed
@sk-

Description

@sk-

Starting with TS 4.1 and also in TS 4.2, it seems that required code is frozen and cannot be reassigned.

We had some code that would require a JSON and conditionally overwrite it, however that code is no longer valid. See example below for a minimal reproduction.

I couldn't find anything in the release notes or breaking changes.

TypeScript Version: 4.1.2

Search Terms:
require json cannot assign let

Code

let a = require('./package.json');
a = {...a};

Expected behavior:
No warnings should be raised for this code. Alternatively at the least I would expect another warning about defining a with let.

Actual behavior:

 Cannot to 'a' because it is not a variable.

Playground Link:
https://www.typescriptlang.org/play?ts=4.2.0-dev.20201121&useJavaScript=true#code/DYUwLgBAhhC8ECcQEcCuBLJAKA5AOgHoAHKAYwGsoBzEPAKwGcB7AOxwEoBuAWACgZ4AbzwioAXz5A

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDesign LimitationConstraints of the existing architecture prevent this from being fixedDomain: JavaScriptThe issue relates to JavaScript specificallycheckJsRelates to checking JavaScript using TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions