-
Notifications
You must be signed in to change notification settings - Fork 199
prefer-array-literal: how can one specify the size of the array? #822
Comments
@ogiexela it depends on case what you want to achieve. Using In general case it is better to use If you really need to create array in this way you can disable error for this line with rule flags e.g. |
What about the case where I already know the exact size that I need the array to be. |
It will depend on your use cases. As mentioned in this tweet you see that If your application creates new big arrays really often and usually uses regular However if big arrays are created only once (or in rare ocassions) and app heavily relies on freqest uses of Anyway - if performance is your concern you should check which of these both approaches will be faster in your case. Main reason for this rule - avoid different/unexpected behavior between cases If you need reallocate array - better to disable rule for that single line (only option available ATM). If I understand correctly - similar rule in ESLint (and migrated rule for TS) allows Array constructors with 1 argument. May be makes sense to add flag that will allow this use cases? @JoshuaKGoldberg what do you think? |
Sure, seems reasonable to add a flag to allow just the case of the constructor with 1 argument. The flag should allow arrays to be created with A few thoughts:
|
* Use only tslint --test for prefer-array-literal rule * Restrict only global objects with Array (fixes #856) * Add allow-single-argument option and check argument types (fixes #822) * Update docs and metadata for prefer-array-literal rule * Apply suggestions related to wording in README.md Co-Authored-By: Josh Goldberg <[email protected]> * Change new option name to allow-size-argument * Use Set for restricted namespaces. Add `self` to restrictions * Fix error message for incorrect size argument * Handle array spread in argument position * Drop node text from all error messages * Handle potential exceptions from TS * Add comment about additional check for SpreadElement
|
@IllusionMH, @JoshuaKGoldberg, would you be open to default this parameter to |
how can one specify the size of the array?
const foo = new Array(10)
?
Originally posted by @ogiexela in #110 (comment)
The text was updated successfully, but these errors were encountered: