Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

prefer-array-literal: Error when using Array in a type annotation #110

Closed
nfriend opened this issue Mar 29, 2016 · 9 comments
Closed

prefer-array-literal: Error when using Array in a type annotation #110

nfriend opened this issue Mar 29, 2016 · 9 comments
Assignees
Milestone

Comments

@nfriend
Copy link

nfriend commented Mar 29, 2016

Perhaps this is the intended behavior, but from the description of the rule, the prefer-array-literal rule looks like its purpose is to disallow using Array when instantiating new arrays:

new Array();
new Array(4,5);
// etc...

However, this rule also disallows using Array as a type parameter, which is one of the recommended ways of declaring Array types in the handbook:

// these throw "Replace generic-typed Array with array literal" linting error

let myArray: Array<MyObject> = [];

interface MyInterface {
    myArray: Array<number>;
}

I expected this rule to throw errors on the first set of examples above but allow the second set.

@HamletDRC
Copy link
Member

Good find! Our coding standard says to prefer the literal syntax, even when declaring the array type.

We can add a parameter to the rule that will let you turn this on.

Why do you prefer

let myArray: Array<MyObject> = []

instead of

let myArray: MyObject[] = [];

The 2nd is shorter and gives your code base one single, consistent way to declare array types. If it were me, I would just follow the rule's advice. But that's a bit unfair because I wrote the rule :)

@nfriend
Copy link
Author

nfriend commented Apr 5, 2016

Thanks for the response, @HamletDRC. I don't necessarily prefer the Array<> syntax, but my team's existing codebase uses it heavily. Adding an option to ignore this error when the syntax is part of a type declaration would make it much easier to integrate this rule into our project.

@HamletDRC HamletDRC self-assigned this Aug 1, 2016
@HamletDRC HamletDRC added this to the 2.0.10 milestone Aug 1, 2016
@ogiexela
Copy link

how can one specify the size of the array?

const foo = new Array(10)
?

@sangheestyle
Copy link
Contributor

@ogiexela You can use const foo = Array(10) instead of const foo = new Array(10). The result will be the same but prefer-array-literal will be disappeared immediately.

https://stackoverflow.com/a/8205709/568901

@JoshuaKGoldberg
Copy link

Oh that sounds like a bug in the rule...

@sangheestyle
Copy link
Contributor

@JoshuaKGoldberg OK. how can I help this issue?

@JoshuaKGoldberg
Copy link

That'd be great! Would you mind filing a new issue describing what you're seeing regarding being allowed to use = Array(10) but not new Array(10)? Including the normal information included with issues (version, code snippets, etc).

@sangheestyle
Copy link
Contributor

@JoshuaKGoldberg Yes, I will do. I just checked which ts-lint actually does prefer-array-literal and I found it is by tslint-microsoft-contrib.

palantir/tslint#2946 (comment)

I am going to test it with up-to-date version of tslint-microsoft-contrib then I will create issue.

@sangheestyle
Copy link
Contributor

@jonascarlsonschmidt Created. Thanks for guiding me.

#839

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants