Angular – Property does not exist on type

I am new to Angular. I took the following example from a book, to get familiar with javascript classes. I try to load it using main.ts . However, I get the following error: error TS2551: Property ‘_weather’ does not exist on type ‘MyClass’. Did you mean ‘weather’? Can anybody explain me why? It should work, it is a copy/paste from a book. I can make it work if I manually add _weather: string; and name: string; declarations. But it shouldn’t be necessary.

 class MyClass {
        constructor(name, weather) {
            this.name = name;
            this._weather = weather;
        }
        set weather(value) {
            this._weather = value;
        }
        get weather() {
            return `Today is ${this._weather}`;
        }
        printMessages() {
            console.log("Hello " + this.name + ". ");
            console.log(this.weather);
        }
    }
    let myData = new MyClass("Adam", "sunny");
    myData.printMessages();

10 thoughts on “Angular – Property does not exist on type”

  1. Error is self explained.

    error TS2551: Property ‘_weather’ does not exist on type ‘MyClass’. Did you mean ‘weather’?

    In your code snipper you are trying to assigned value to the property that don’t exists.

    this._weather is arranged to be private since you create setter/getter to it.

    In typescript you should name private variables with underscore in order to avoid duplicates

    class MyClass {
        private _weather: any;
        private _name: any;
        constructor(name: any, weather: any) {
            this.name = name;
            this.weather = weather;
        }
        set name(value) {
            this._name = value;
        }
        get name() {
            return `Name is ${this._name}`;
        }
    
        set weather(value) {
            this._weather = value;
        }
        get weather() {
            return `Today is ${this._weather}`;
        }
        printMessages() {
            console.log("Hello " + this.name + ". ");
            console.log(this.weather);
        }
    }
    let myData = new MyClass("Adam", "sunny");
    myData.printMessages();

    If you don’t want to specify setters/getters then write constructor like so

    class MyClass {
    
        constructor(private name: any, private weather: any) {
            this.name = name;
            this.weather = weather;
        }
    
        printMessages() {
            console.log("Hello " + this.name + ". ");
            console.log(this.weather);
        }
    }
    let myData = new MyClass("Adam", "sunny");
    myData.printMessages();
    Reply
  2. class MyClass {
    
           private name: string;
           private _weather: string;
    
           constructor(name, weather) {
               this.name = name;
               this._weather = weather;
           }
    
           set weather(value) {
               this._weather = value;
           }
    
           get weather() {
               return `Today is ${this._weather}`;
           }
    
           printMessages() {
               console.log("Hello " + this.name + ". ");
               console.log(this._weather);
           }
    
       }
       let myData = new MyClass("Adam", "sunny");
       myData.printMessages();
    

    Hiya, if I’m understanding this correct. You did not have the properties defined in the class. I have added them as private properties expecting the type "string".

    Also, I updated the printMessages() method to return _weather as opposed to weather in your example.

    I haven’t tested this, but I wish you luck in your project.

    Reply

Leave a Comment