Skip to content

Commit 573af81

Browse files
authored
Add caveat documentation for array.fill behavior (#167)
1 parent 8a13ea6 commit 573af81

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/caveats.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ Even though iterating over object keys with `for ... in` does not guarantee orde
113113

114114
**Note:** If a specific order is required, it is better to use ordered collections like arrays instead.
115115

116+
### Array.fill 'end' parameter
117+
118+
In the ECMAScript spec for [Array.prototype.fill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill) is stated that if `end >= array.length`, then array.length is used instead. In Lua, the concept of array length is a bit more complex (see [Array Length](#array-length)). Therefore we decided to use just fill the array until the end index - whatever is provided - regardless of the original array length.
119+
120+
As a bonus this serves as a standin for the creation of an array of a specific length via `new Array(length)` (not implemented). With this modification to `Array.fill`, you can instead use `([] as number[]).fill(defaultValue, 0, length)`.
121+
122+
```ts
123+
// const myNewArray = new Array(5); - new Array is not supported by typescript-to-lua
124+
125+
// Instead, use:
126+
127+
const myNewArray2 = ([] as number[]).fill(0, 0, 5); // Using this will create an array with 5 0's
128+
```
129+
116130
### Iterating an array with `for ... in`
117131

118132
Not allowed. Use a `for of` loop instead to iterate over an array.

0 commit comments

Comments
 (0)