What does it do to define a property outside of the Constructor?

I first noticed this while learning TypeScript, but I have never seen anyone doing this in Pure javascript, but when I tested it I realized that it can also be done with pure javascript.

class Department {
    name: string
    arrExam: string[] = []
    constructor(name:string) {
        this.name = name

}

What good is it for us to build it outside rather than inside of the founder, or whatever we do would benefit us to define it this way?

If something does not rely on an argument then it does need to be in the constructor.

class square {
    constructor(shape) {
        this.shape = shape;
        //isSquare does not need to be in here as it does 
        //rely an argument
        this.isSquare = true;
    }
    //you no longer need to use this outside of the constructor
    isNotSquare = false;
}

This is very common, I use it all the time in React, with JS or TS. I think it was a proposal for JS but was implemented with babel. I assume it’s been approved by now.

You need to define what the type of object properties are in TS. You have to define interfaces in TS, that’s a core part of how it does what it does.

This shouldn’t work, it should be a compilation error – what is the type of this.name? It’s unknown, then you’re trying to assign a string to it:

class Department {
  constructor(name: string) {
    this.name = name;
  }
}

This works, the property has the type string defined so you can assign a value to it provided that value is of type string:

class Department {
  name: string

  constructor(name: string) {
    this.name = name;
  }
}

JS isn’t statically typed, so it doesn’t matter. The syntax is valid JS. You’re initialising a property as undefined, same as initialising an undefined variable – like let name;. It’s just that there’s no point, you can assign whatever you want to this.name, JS won’t care. The following is fundamentally impossible in Typescript if name defined as a string, but it’s perfectly valid JS:

class Department {
  name;

  constructor(name) {
    this.name = name;
  }
}

const weirdlyNamedDept = new Department([true, 123, "foo", /ABC/]);

As to why you can define properties like that. Say you want to create a class with some prefilled values in the object. Like:

class Count {
  constructor () {
    this.counter = 0;
    this.timer;
  }

  start() {
    this.timer = setInterval(() => {
      console.log(this.counter);
      this.counter++;
    }, 1000);
  }

  stop () {
    clearInterval(this.timer);
    this.counter = 0;
  }
}

You don’t need to define the constructor here, you aren’t passing in anything that needs to set it up, can just use class fields:

class Count {
  counter = 0;
  timer;

  start() {
    this.timer = setInterval(() => {
      console.log(this.counter);
      this.counter++;
    }, 1000);
  }

  stop () {
    clearInterval(this.timer);
    this.counter = 0;
  }
}

Another reason for using class fields is that it looks like private class fields will be required to be declared up-front as a field declaration instead of in the constructor.

In the constructor, you’re assigning a value to a class property. In Javascript, you can define a class property in the constructor like so:

class Department {
  name
  constructor(name) {
    this.name = name
    this.someOtherProp = 2
  }

In Typescript, you cannot because the syntax checker will complain that you did not define the property:

class Department {
  name: string
  constructor(name: string) {
    this.name = name;
    this.someOther = 2
  }
}
index.ts:5:10 - error TS2339: Property 'someOther' does not exist on type 'Department'.

5     this.someOther = 2
           ~~~~~~~~~