Do getters / setters work properly yet? This errors no matter what

Topics: General
Oct 10, 2012 at 3:59 PM


Can anyone see something wrong with the following class? When I compile I get the "Error 1":

Error 1 The command ""C:\Program Files (x86)\Microsoft SDKs\TypeScript\\tsc" --out kiwibomb.js "D:\wamp\www\kiwi.js\typescript\KiwiJS\KiwiJS\app.ts"" exited with code 1. D:\wamp\www\kiwi.js\typescript\KiwiJS\KiwiJS\KiwiJS.csproj 75 5 KiwiJS

class BaseCircle {

    constructor (x: number = 0, y: number = 0, diameter: number = 0) {

		return this.setTo(x, y, diameter);


    private _diameter: number = 0;
    private _radius: number = 0;

    x: number = 0;
    y: number = 0;

	get diameter ():number {

		return this._diameter;


	set diameter (value:number) {

		if (!isNaN(value) && value > 0)
			this._diameter = value;
			this._radius = value * 0.5;


	get radius ():number {

		return this._radius;


	set radius (value:number) {

		if (!isNaN(value) && value > 0)
			this._radius = value;
			this._diameter = value * 2;


	setTo (x:number, y:number, diameter:number):BaseCircle {

		if (!isNaN(x) && !isNaN(y) && !isNaN(diameter))
			this.x = x;
			this.y = y;
			this._diameter = diameter;
			this._radius = diameter * 0.5;

		return this;



If I comment out the diameter and radius getters and setters, and just make setTo change the values directly (removing their private status) then it works fine. At a bit of a loss as to why it errors. There's no resulting JS as it's a compilation error.

Oct 10, 2012 at 4:22 PM
Edited Oct 10, 2012 at 4:22 PM

compiles just fine for me, VS doesn't complain either. I don't think the return in the constructor should be there, though. And the isNaN checks are not necessary as you set the variables to be numbers. Just provide default values instead.

Oct 10, 2012 at 5:29 PM

The return is for chaining, but that's really strange that it works (but also great I guess!).

Going to try a brand new VS project now to see what happens.

Oct 10, 2012 at 5:44 PM
Edited Oct 10, 2012 at 5:44 PM

Well, a constructor always returns a new instance anyway, or am I wrong?

Oct 10, 2012 at 5:56 PM

Ok it still doesn't work. It must be something to do with my VS project.

I went into VS 2012 (Pro). File -> New -> Project. Picked "HTML Application with TypeScript" from the Visual C# category and gave it a name.

I then added the BaseCircle.ts file (via Add Existing) and just threw this into the app.ts file that it creates you by default (the one with the Greeter class in):

var test:BaseCircle = new BaseCircle(100,100,100);

I also made sure I had the reference path set and added the BaseCircle.js script tag to the default.html file.

Hit F5 and nothing but the compiler error "error code 1".

Comment out the test var line above and it compiles and runs just fine.

Have I missed something in the project set-up perhaps?

Oct 10, 2012 at 6:27 PM

Ok I figured it out. I should have tried it earlier - I tried compiling just BaseCircle.ts from the command-prompt and then I got a meaningful error! "Property accessors are only available when targeting ES5 or greater".

Bingo! Added the --target ES5 to my csproj file and now it compiles :)