TypeScript Tutorial

TypeScript Constructors

A constructor runs when an object is created. Use it to set fields so the object starts valid.

Code that runs at creation

A constructor is a special method named constructor. The compiler calls it when you writenew Point() or new Point(3, 4). That is the moment to give fields real values, so you never print an uninitialized number.

You already created objects with new Player(). If you write no constructor, TypeScript still provides an empty one. Write your own when the object must start in a known state, or when each object needs different starting values.

A constructor with no parameters

A constructor with an empty parameter list runs for new Point(). Here it sets x andy to 0 so the point is usable at once. The fields are declared on the class; the constructor assigns them through this.

Example

class Point {
  public x: number;
  public y: number;

  constructor() {
    this.x = 0;
    this.y = 0;
  }
}

const origin: Point = new Point();
console.log(origin.x + " " + origin.y);

Under strict, a field with no starter value must be assigned in the constructor. That is whyx and y have no = 0 on the field line: the constructor is the assignment.

Constructor with parameters

Pass values into the constructor when each object needs a different start. new Point(3, 4) calls the version that takes two numbers. You do not write p.x = 3; afterward unless you want to change it later.

Example

class Point {
  public x: number;
  public y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

const p: Point = new Point(3, 4);
console.log(p.x + " " + p.y);

Compile this in /typescript/try. Create two points with different arguments and print both.

Parameter properties

After you can write the plain constructor, TypeScript offers a shorter form. Putpublic, private, or protected in front of a constructor parameter. That both declares the field and assigns the argument. The body can stay empty.

Example

class Point {
  constructor(public x: number, public y: number) {}
}

const p: Point = new Point(3, 4);
console.log(p.x + " " + p.y);

constructor(public x: number, public y: number) is the same type as the previous listing. Use the short form once the long form is clear. This tutorial still writes the long form when the constructor has extra work to do besides storing the arguments.

One constructor body

A TypeScript class has one constructor implementation. You do not write two constructor bodies the way C++ overloads constructors. To allow both new Point() and new Point(3, 4), give the parameters defaults.

Example

class Point {
  public x: number;
  public y: number;

  constructor(x: number = 0, y: number = 0) {
    this.x = x;
    this.y = y;
  }
}

const origin: Point = new Point();
const p: Point = new Point(3, 4);
console.log(origin.x + " " + origin.y);
console.log(p.x + " " + p.y);

After you add constructor(x: number, y: number) with no defaults, the linenew Point() fails to compile. Provide defaults, or always pass the arguments.

Constructors do not return a value

Do not write return with a value in a constructor. Finish by leaving the fields set.new yields the object. Next: who is allowed to read and write those fields.

FAQ: TypeScript Constructors

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript constructors examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript constructors in this TypeScript TypeScript lesson (TypeScript Constructors).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.