0

i am trying to create a variable with Object type and initialised it with a property. but when i try to access that property it shows error 'Property ____ does not exist on type Object'. I had already searched for this and i found there are 3 type object , Object and {}. I can access my properties with {} but not with object and Object.

export class customDirective {
    configg:Object={
        qSelector:'.card-text'
    };
    @HostListener('mouseover') onmouseover(){
        var element =this.el.nativeElement.querySelector(this.configg.qSelector);
        this.ren.setElementStyle(element, 'display', 'block');
        this.isHovering = true;
    }
}

2 Answers 2

1

Access property of object type using [ ]

i.e. this.configg['qSelector'] not this.configg.qSelector

export class customDirective {
    configg:Object={
        qSelector:'.card-text'
    };
    @HostListener('mouseover') onmouseover(){
        var element =this.el.nativeElement.querySelector(this.configg['qSelector']);
        this.ren.setElementStyle(element, 'display', 'block');
        this.isHovering = true;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If this answer is helpful then please approve it.
0

Object is an actual class: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object. It has certain properties defined, and qSelector is not one of it.

To define an arbitrary object that can have any properties set without TypeScript complaining, use the any type (or leave out the type definition altogether):

configg: any = {
    qSelector:'.card-text'
};

2 Comments

And you loose all type checking by doing this. There is no point to use TypeScript when you type everything as any.
Sure. If you want to re-use your object, profit from type checking during compilation, have proper code completion available in your favorite IDE, ..., you should probably type it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.