- Learn React with TypeScript 3
- Carl Rippon
- 451字
- 2021-06-10 19:16:33
Arrays
Arrays are structures that TypeScript inherits from JavaScript. We add type annotations to arrays as usual, but with square brackets at the end to denote that this is an array type.
Let's take a look at an example:
- Let's declare the following array of numbers in the TypeScript playground:
const numbers: number[] = [];
Here, we have initialized the array as empty.
- We can add an item to the array by using the array's push function. Let's add the number 1 to our array:
numbers.push(1);
We used const to declare the numbers variable and was able to change its array elements later in the program. The array reference hasn't changed – just the elements within it. So, this is fine with the TypeScript compiler.
- If we add an element with an incorrect type, the TypeScript compiler will complain, as we would expect:
- We can use type inference to save a few keystrokes if we declare an array with some initial values. As an example, if we type in the following declaration and hover over the numbers variable, we'll see the type has been inferred as number[].
const numbers = [1, 3, 5];
- We can access an element in an array by using the element number in square brackets. Element numbers start at 0.
Let's take an example:
- Let's log out the number of elements under the numbers variable declaration, as follows:
console.log(numbers[0]);
console.log(numbers[1]);
console.log(numbers[2]);
- Let's now click the Run option on the right-hand side of the TypeScript playground to run our program. A new browser tab should open with a blank page. If we press F12 to open the Developer tools and go to the console section, we'll see 1, 3, and 5 output to the console.
- There are several ways to iterate through elements in an array. One option is to use a for loop, as follows:
for (let i in numbers) {
console.log(numbers[i]);
}
If we run the program, we'll see 1, 3, and 5 output to the console again.
- Arrays also have a useful function for iterating through their elements, called forEach. We can use this function as follows:
numbers.forEach(function (num) {
console.log(num);
});
- forEach calls a nested function for each array element, passing in the array element. If we hover over the num variable, we'll see it has been correctly inferred as a number. We could have put a type annotation here, but we have saved ourselves a few keystrokes:
Arrays are one of the most common types we'll use to structure our data. In the preceding examples, we've only used an array with elements having a number type, but any type can be used for elements, including objects, which in turn have their own properties.
推薦閱讀
- ClickHouse性能之巔:從架構設計解讀性能之謎
- Django+Vue.js商城項目實戰
- Twilio Best Practices
- 算法訓練營:入門篇(全彩版)
- Web Development with Django Cookbook
- SQL for Data Analytics
- Functional Programming in JavaScript
- MongoDB,Express,Angular,and Node.js Fundamentals
- 圖數據庫實戰
- Android移動開發案例教程:基于Android Studio開發環境
- 零基礎學C語言第2版
- 深入實踐Kotlin元編程
- Cocos2d-x Game Development Blueprints
- 小程序從0到1:微信全棧工程師一本通
- 算法圖解