I'm using ES6 classes in typescript with the following:
class Camera {
    constructor(ip) {
        this.ip = ip;
    }
}
I get back this error, though it still seems to compile:
Property 'ip' does not exist on type 'Camera'.
If I define the type:
this.ip: string = ip;
I get back:
';' expected.
How should I format the class to get rid of both errors?
Either declare the property on the class first:
class Camera {
    ip: string;
    constructor(ip: string) {
        this.ip = ip;
    }
}
Or declare it on the constructor parameter (the preferred approach), just add an access modifier (public or private) to the parameter to indicate it is a property:
class Camera {
    constructor(public ip: string) {
        // note no explicit assignment
    }
}
You have tried to access the property "ip" without definng it in the class itself. The constructer(ip){} is called when you call the Camera class and it search for the ip property.Since you have not defined it in the class gives an error
use this way. best wishes.
class Camera {
 private ip: string;  // declare your variable first with the type
  constructor(ip) {
    this.ip = ip;
  }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With