-2

I'm working on a project in a company and there's a variable declared like this

  chainTypeList: any[]=[];

and I'm not able to access it's values like this.

this.chainTypeList.chainTypeCode;

It shows this error : Property 'chainTypeCode' does not exist on type 'any[]'.

3 Answers3

2

This is looks like typescript's typing.

This means that this is an array of any value. An it's initilized as an empty one.

The any value literally means that you can push any value and value type into this array.

You can't access it by using this:

this.chainTypeList.chainTypeCode;

because it's an array and not an object. That's why you get the error:

Property 'chainTypeCode' does not exist on type 'any[]'.

It should only be something like this:

this.chainTypeList

or

this.chainTypeList[0]

If you want a specific position of the array. Just change the number for the position you want to obtain.

goediaz
  • 622
  • 6
  • 19
1

chainTypeList: any[]=[];

It is written as TypeScript not JavaScript. You cannot do this if it is pure JavaScript. It means that variable chainTypeList is declared as an array type any (can be any type), which is initialized as an empty array.

and by calling

this.chainTypeList.chainTypeCode;

you are trying to access property named chainTypeCode in the object chainTypeList. However, the program will not be able to find such property because, as it is declared earlier, the variable is an empty array. It does not have the property you are looking for hence the error

Property 'chainTypeCode' does not exist on type 'any[]'.

holydragon
  • 6,158
  • 6
  • 39
  • 62
0

What does any[]=[] datatype mean in TypeScript?

It's not a datatype.

The any[] part is a datatype. The = [] part is called an "assignment". An assignment binds a value to a name, e.g. in this case, it binds the value [] (an empty array) to the name chainTypeList, meaning that from this point on, dereferencing the name chainTypeList will evaluate to an empty array.

this.chainTypeList.chainTypeCode;

It shows this error : Property 'chainTypeCode' does not exist on type 'any[]'.

Arrays don't have a property called chainTypeCode, so the error is correct.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653