Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object literal's property 'children' implicitly has an 'any[]' type

How can I set the myOtherKey property to have any?

Typescript error ..

Object literal's property 'children' implicitly has an 'any[]' type

Object initialization ..

let myObject = {
  myKey: 'someValue',
  myOtherKey: []
};
like image 357
bobbyrne01 Avatar asked Aug 18 '17 13:08

bobbyrne01


2 Answers

What you could do is just assert yourself what the expected type of that array should be in order to overwrite the compiler's resolution of the any type.

let myObject = {
  myKey: 'someValue',
  myOtherKey: [] as string[]
};
like image 183
toskv Avatar answered Oct 24 '22 09:10

toskv


You can also tell Typescript that you don't care about implicit any by setting:

 "noImplicitAny": false,

in your tsconfig.json.

If you don't want to set it off for the whole project, you can also add this line before 'myOtherKey' line:

// @ts-ignore

Usually toskv answer is the way to go, but sometimes it's just an overhead to explain everything to typescript (like when you're mocking some object and you want empty arrays saved on some properties).

like image 9
Patryk Wlaź Avatar answered Oct 24 '22 10:10

Patryk Wlaź