Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object literal's property implicitly has an 'any[]' type - Angular 2

Tags:

angular

I recently upgraded angular 2 to stable version & suddenly I started getting this error in webpack watcher Object literal's property 'avatars' implicitly has an 'any[]' type. Here is the line which is giving that error: private selectedContact = {'jcf': {'avatars': [], 'fullname': ''}, meta: []}; Its giving same error for meta.

like image 433
Dheeraj Agrawal Avatar asked Dec 27 '16 07:12

Dheeraj Agrawal


1 Answers

With the new typescript update come new rules and flags. One of these flags is the noImplicitAny flag. This makes sure you don't initialise a variable like this:

let avatars = [];

You can either change your tsconfig.json to no longer mark this as an error by using:

{
    noImplicitAny: false
}

or you can create an interface/class which represents your selectedContact

export interface Contact {
    jcf: ContactDetail;
    meta: any[];
}

And another interface:

export interface ContactDetail {
    avatars: any[];
    fullname: string;
}

Now you can assign a Contact interface to your selectedContact property:

private selectedContact: Contact = {...};

Or, which is not very nice, you can explicitly assign it to any[]:

let avatars: any[] = [];
like image 196
Poul Kruijt Avatar answered Oct 23 '22 02:10

Poul Kruijt