Reputation:
I have an angular project, In the project, I have a service it returns an object, And In the app component, I'm using that object.
theobject.profileImg
Everything works properly, But Idm shows this err. " Property 'profileImg' does not exist on type 'object'. "
I'm using vsCode
Upvotes: 0
Views: 56
Reputation: 4346
Generally it is always better to prepare types properly and avoid situations like you have.
for example:
export interface SomeInterface{
public profileImg: any;
}
const anotherVar: SomeInterface = theobject;
anotherVar.profileImg; // this will be fine
Another option is use any
type:
(company as any).profileImg
Or
theobject['profileImg']
Also, if you just want to skip it in one place use @ts-ignore
// @ts-ignore: some description
theobject.profileImg
Or disable linting at all.
Upvotes: 0
Reputation: 3236
using type Object
shows this kind of error. The best practice is to create your own interface (which will have the attribute profileImg
) to enable autocompletion. If you choose not to do so, use type any
instead
Upvotes: 1
Reputation: 699
In template use like:
theobject?.profileImg
or
theobject['profileImg']
but generally better add model to your object, like:
theobject: IObject;
and
interface IObject {
profileImg: string;
id: number;
//and more
}
Upvotes: 1