|
3 | 3 | * Licensed under the MIT License.
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -const arr2 = [1, 2, 3]; |
7 |
| -const value2 = arr2[0]; // This should not report an error |
| 6 | +/* |
| 7 | + * Array Access Tests |
| 8 | + */ |
| 9 | + |
| 10 | +/* Basic array access */ |
| 11 | +const numberArray = [1, 2, 3]; |
| 12 | +const stringArray = ["a", "b", "c"]; |
| 13 | +const mixedArray = [1, "b", true]; |
| 14 | + |
| 15 | +// Direct index access |
| 16 | +const firstNumber = numberArray[0]; // ok: Accessing array with numeric literal |
| 17 | +const lastString = stringArray[2]; // ok: Accessing array with numeric literal |
| 18 | +const middleMixed = mixedArray[1]; // ok: Accessing array with numeric literal |
| 19 | + |
| 20 | +// Variable index access |
| 21 | +const index = 1; |
| 22 | +const numberByVar = numberArray[index]; // ok: Accessing array with numeric variable |
| 23 | +const stringByVar = stringArray[index]; // ok: Accessing array with numeric variable |
| 24 | + |
| 25 | +/* Readonly arrays */ |
| 26 | +const readonlyArray: ReadonlyArray<number> = [1, 2, 3]; |
| 27 | +const readonlyFirst = readonlyArray[0]; // ok: Accessing readonly array |
| 28 | +const readonlyByVar = readonlyArray[index]; // ok: Accessing readonly array with variable |
| 29 | + |
| 30 | +/* Tuple types */ |
| 31 | +const tuple: [string, number, boolean] = ["hello", 42, true]; |
| 32 | +const tupleFirst = tuple[0]; // ok: Accessing tuple element |
| 33 | +const tupleSecond = tuple[1]; // ok: Accessing tuple element |
| 34 | +const tupleByVar = tuple[index]; // ok: Accessing tuple with variable |
| 35 | + |
| 36 | +/* Array-like objects */ |
| 37 | +const arrayLike = { |
| 38 | + length: 3, |
| 39 | + 0: "zero", |
| 40 | + 1: "one", |
| 41 | + 2: "two" |
| 42 | +}; |
| 43 | +const arrayLikeFirst = arrayLike[0]; // ok: Accessing array-like object with numeric index |
| 44 | + |
| 45 | +/* Array methods that return arrays */ |
| 46 | +const slicedArray = numberArray.slice(1); // Creates new array |
| 47 | +const slicedElement = slicedArray[0]; // ok: Accessing sliced array |
| 48 | + |
| 49 | +/* Nested arrays */ |
| 50 | +const nested = [[1, 2], [3, 4]]; |
| 51 | +const nestedElement = nested[0][1]; // ok: Accessing nested array |
| 52 | +const nestedByVar = nested[index][index]; // ok: Accessing nested array with variables |
| 53 | + |
| 54 | +/* Array with optional elements */ |
| 55 | +const sparseArray: (number | undefined)[] = [1, undefined, 3]; |
| 56 | +const sparseElement = sparseArray[1]; // ok: Accessing potentially undefined element |
| 57 | + |
| 58 | +/* Array destructuring */ |
| 59 | +const [first, second] = numberArray; // ok: Array destructuring |
| 60 | + |
| 61 | +/* Array access with expressions */ |
| 62 | +const expressionIndex = 1 + 1; |
| 63 | +const elementByExpression = numberArray[expressionIndex]; // ok: Accessing with expression |
| 64 | +const elementByComputation = numberArray[index + 1]; // ok: Accessing with computation |
| 65 | + |
| 66 | +/* TypedArray access */ |
| 67 | +const typedArray = new Int32Array([1, 2, 3]); |
| 68 | +const typedElement = typedArray[0]; // ok: Accessing typed array |
| 69 | + |
| 70 | +/* Array subclass */ |
| 71 | +class CustomArray extends Array<number> {} |
| 72 | +const customArray = new CustomArray(1, 2, 3); |
| 73 | +const customElement = customArray[0]; // ok: Accessing custom array subclass |
| 74 | + |
| 75 | +/* Generic array types */ |
| 76 | +function accessGenericArray<T>(arr: T[], index: number): T { |
| 77 | + return arr[index]; // ok: Accessing generic array |
| 78 | +} |
| 79 | + |
| 80 | +/* String access (string is array-like) */ |
| 81 | +const str = "hello"; |
| 82 | +const char = str[0]; // ok: Accessing string character |
| 83 | + |
| 84 | +/* Array-like DOM collections */ |
| 85 | +const htmlCollection = document.getElementsByTagName("div"); |
| 86 | +const firstElement = htmlCollection[0]; // ok: Accessing HTML collection |
| 87 | + |
| 88 | +/* Iterator-based access */ |
| 89 | +for (let i = 0; i < numberArray.length; i++) { |
| 90 | + const element = numberArray[i]; // ok: Accessing in loop |
| 91 | +} |
| 92 | + |
| 93 | +/* Array with union type */ |
| 94 | +const unionArray: (string | number)[] = ["a", 1, "b", 2]; |
| 95 | +const unionElement = unionArray[0]; // ok: Accessing union type array |
| 96 | + |
| 97 | +/* Readonly tuple */ |
| 98 | +const readonlyTuple: readonly [number, string] = [1, "two"]; |
| 99 | +const readonlyTupleElement = readonlyTuple[0]; // ok: Accessing readonly tuple |
0 commit comments