2

I want to use classes in my Node Application but I wonder whether there is a way (like in typescript) to minimize class definitions, for example:

class Rectangle(

    constructor(height, width){
        this.height = height;
        this.width = width;
    }
)

to something like

class Rectangle(

    constructor(height, width){}
)

so I do not need to assign the values from the constructor to variables with exactly the same name.

CNIS
  • 101
  • 1
  • 7
  • 2
    As far as I know, the answer is no; that's a feature of TypeScript only. – Ken Y-N Nov 09 '20 at 08:05
  • @KenY-N Thank you for the information. I read that you can use TypeScript classes in Node applications - I may try to work with TS classes in my Node application if that should not be avoided. – CNIS Nov 09 '20 at 08:07

1 Answers1

1

No, parameter properties are a typescript only feature. As you can see in this example on ts-playground, a class like

class Rectangle{
   constructor(private height:number, public width:number){ }
}

gets transpiled into:

"use strict";
class Rectangle {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

You can, however, just use TypeScript in your NodeJS app - given that you set up a build-step where you transpile/compile your typescript code into javascript. Alternatively you can use a library like ts-node to run your code.

eol
  • 23,236
  • 5
  • 46
  • 64