Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left-hand side of assignment expression cannot be a constant or a read-only property

When I use this line on my Express server, it works well in TypeScript 1.x

mongoose.Promise = global.Promise;

( The usage of mongoose.Promise = global.Promise; is from the mongoose document )

After updating to TypeScript 2.x, it shows this error in the terminal, and won't let me start the server.

Left-hand side of assignment expression cannot be a constant or a read-only property.

How can I solve this? Thanks

like image 722
Hongbo Miao Avatar asked Aug 07 '16 20:08

Hongbo Miao


1 Answers

This is because in es6 all module's variables are considered constants.

https://github.com/Microsoft/TypeScript/issues/6751#issuecomment-177114001

In TypeScript 2.0 the bug (of not reporting this error) was fixed.

Since mongoose is still using the commonjs - var mongoose = require("mongoose") - not the es6 import syntax (which is used in the typings), you can suppress the error by assuming the module is of type any.

WORKAROUND:

(mongoose as any).Promise = global.Promise;
like image 164
derenio Avatar answered Oct 12 '22 09:10

derenio