Sn&#230;bj&#248;rn - 1 year ago 53
TypeScript Question

# TypeError when class has static member name

I have the following TypeScript class

module Test {
"use strict";

class Foo {
public static name = "foo";
}
}


Pretty simple.

But when run in Chrome I get the following error:

Uncaught TypeError: Cannot assign to read only property 'name' of function 'function Foo() { }'

Here's the generated javascript:

var Test;
(function (Test) {
"use strict";
var Foo = (function () {
function Foo() {
}
Foo.name = "foo";
return Foo;
}());
})(Test || (Test = {}));

If I use a different name then
name
I don't get an error.

module Test {
"use strict";

class Foo {
public static huh = "foo";
}
}


What is going on?

The problem seems to be that you are trying to write to Function.name. In the code that you wrote initially, you can see in the compiled code that you are changing Foo.name.
  function Foo(){}

The error is because you are trying to change this function's property, and are not allowed to do so (read-only property ..). More information about why (and how) you are not allowed to do so can be found here. Thanks to @ssube for providing this link in the comments