John Abraham
John Abraham

Reputation: 18791

How to initialize a const field in constructor in typescript?

I don't know if theres even a value in this. I'm just curious if it is possible?

Question:

if it's possible and what are the syntax for

initializing a const field in constructor in typescript?`

export class Gulpfile {
  private dist: string;
  private src: string;

  constructor(){
    const this.dist = './dist/';
    this.src = './src'
  }
 }

Upvotes: 6

Views: 2658

Answers (1)

toskv
toskv

Reputation: 31600

You can't have a constant class member.

Mainly because class members are always referenced by reference via the this keyword, and that reference can always be changed.

It's not supported in ES6 either.

Upvotes: 8

Related Questions