Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript variable assignment causes linting error

Tags:

typescript

I have a question about the following use of a typescript array. It all works fine but when I run it through the linter I get the following error, my assignment is obviously wrong.

Array type using 'Array' is forbidden for simple types. Use 'T[]' instead.

 export let data = [
  {
    "property": "value"
  }
 ];

export interface myInterface {
    property: string;
};

protected _collection: Array<myInterface>;

Any assistance is appreciated.

like image 925
Jimi Avatar asked Jan 17 '17 09:01

Jimi


2 Answers

The linter probably just wants you to do:

protected _collection: myInterface[];

The types myInterface[] and Array<myInterface> are equivalent, but the linter seems to prefer the first.

like image 85
Nitzan Tomer Avatar answered Nov 15 '22 20:11

Nitzan Tomer


Seems like the error stacktrace is not complete. The complete sentence is

Array type using 'Array<T>' is forbidden. Use 'T[]' instead.

You have to code format it on SO. And the reason for the linter is here in the source code of palantir

https://github.com/palantir/tslint/blob/master/src/rules/arrayTypeRule.ts#L81-L82

They want you to avoid using Array<T> in general. Instead you have to use it like this

protected _collection: myInterface[];

This is more like a preference from them.

like image 2
Murat Karagöz Avatar answered Nov 15 '22 18:11

Murat Karagöz