Warum können wir nicht definieren, ein const-Feld in TypeScript Klasse, und warum static readonly funktioniert nicht?

Möchte ich const Schlüsselwort in meinem Programm.

export class Constant {

    let result : string;

    private const CONSTANT = 'constant'; //Error: A class member cannot have the const keyword.

    constructor () {}

    public doSomething () {
        if (condition is true) {
           //do the needful
        }
        else
        {
            this.result = this.CONSTANT; //NO ERROR
        }


    }
}

Question1: warum die Schüler nicht haben, wird das const-Schlüsselwort in typescript?

Question2: Wenn ich

static readonly CONSTANT = 'constant';

zuweisen und es in

this.result = this.CONSTANT;

zeigt es Fehler. warum ist das so?

Ich habe diesen Beitrag Wie implementieren Sie die Klasse Konstanten, die in typescript?, aber nicht bekommen, die Antwort, warum Typoskript ist die Darstellung dieser Art von Fehler mit const Stichwort.

InformationsquelleAutor Aditya | 2017-10-17

Schreibe einen Kommentar