Typescript not compiling class properties

Viewed 222

I have a problem with typescript. The problem is that it suddenly stopped compiling class properties to the .js file. It compiles everything, the functions the constructor but the properties aren't there. Server.ts

export class Server{
    id: String;
    prefix: String;
    constructor(){
        
    }
    copy(object){
        object = JSON.parse(JSON.stringify(object));
        console.log(this);
        
        for (const k in object) {
            if(this.hasOwnProperty(k))
                this[k] = object[k];
        }
        console.log(this);
         
    }

}

Server.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = void 0;
class Server {
    constructor() {
    }
    copy(object) {
        object = JSON.parse(JSON.stringify(object));
        console.log(this);
        for (const k in object) {
            if (this.hasOwnProperty(k))
                this[k] = object[k];
        }
        console.log(this);
    }
}
exports.Server = Server;

Scenario.ts

import { percentageChance } from "../utilities/percentageChance";
import { Champion } from "./Champion";

export class Scenario {
    name: String;
    description: String;
    champion: Champion;
    choices: string[];
    outcome: number[];
    final: string[];

    constructor(name: String, description: string | String, champion: Champion, choices: string[], outcome: number[], final: string[]) {
        this.name = name;
        this.description = description;
        this.champion = champion;
        this.choices = choices;
        this.outcome = outcome;
        this.final = final;
    }

    calculateOutcome(choiceIndex : number) {
        if(percentageChance(this.choices, this.outcome) == this.choices[choiceIndex]){
            return true;
        }
        return false;
    }


}

Scenario.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scenario = void 0;
const percentageChance_1 = require("../utilities/percentageChance");
class Scenario {
    constructor(name, description, champion, choices, outcome, final) {
        this.name = name;
        this.description = description;
        this.champion = champion;
        this.choices = choices;
        this.outcome = outcome;
        this.final = final;
    }
    calculateOutcome(choiceIndex) {
        if (percentageChance_1.percentageChance(this.choices, this.outcome) == this.choices[choiceIndex]) {
            return true;
        }
        return false;
    }
}
exports.Scenario = Scenario;

Has anyone had anything like this happened? If yes then how should it fix this?

1 Answers

I think that one is easy, as JS is a dynamically typed language no property that was not assigned a value will be created. So typescript will leave those properties to be created on-demand.

If you put a default value in class properties, it makes them transpile to JS on compile time.

export class Scenario {
name: String = '';
description: String = '';
champion: Champion = new Champion();
choices: string[] = [];
outcome: number[] = [];
final: string[] = [];
//...
}

Had the same problem using a method to copy properties of the same values from another object, since my properties do not exist (because of absent default value) nothing was copied...

Related