TypeScript: TypeError: Cannot assign to read only property 'constructor'
With the TypeScript 1.8 change of 'use strict'
, the emitted JavaScript (specifically __extends
helper) can be invalid.
TypeScript:
import makeError = require('make-error')
export class SubError extends makeError.BaseError {}
Emitted JavaScript:
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var makeError = require('make-error');
var SubError = (function (_super) {
__extends(SubError, _super);
function SubError() {
_super.apply(this, arguments);
}
return SubError;
}(makeError.BaseError));
exports.SubError = SubError;
Error:
/Users/blakeembrey/Projects/tmp/test.js:4
function __() { this.constructor = d; }
^
TypeError: Cannot assign to read only property 'constructor' of Error
at BaseError.__ (/Users/blakeembrey/Projects/tmp/test.js:4:38)
at __extends (/Users/blakeembrey/Projects/tmp/test.js:5:80)
at /Users/blakeembrey/Projects/tmp/test.js:9:5
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 22 (16 by maintainers)
Commits related to this issue
- chore: Lock typescript version to 1.7.5 Typescript 1.8.x can emit invalid JS: https://github.com/Microsoft/TypeScript/issues/6887 — committed to jteplitz/angularfire2 by jteplitz 8 years ago
We would have the same issue if someone were to write the following:
Even with this @blakeembrey’s proposed change, we would still error in the following case:
This is because we assign methods on the prototype, rather than calling
Object.defineProperty
.