-
Notifications
You must be signed in to change notification settings - Fork 0
Array Types
When dealing with with array types in TypeScript, it ususally infers the the type in which the array will be constructed with, when the array is declared AND initialized.
Though, the best practices suggest that we declare those manually, as it'll be detailed below.
To define array types formed by any pre-defined type, the syntax pretty straight: it consist on adding a pair of opening and closing square bracket to the type of the variable...
type Grades = number[];
type Student = {
name: string;
age: number;
grades: Grades;
};
type Classroom = Student[];
const grades1: Grades = [10, 9, 10, 5];
const grades2: Grades = [10, 8, 8, 4];
const student1: Student = {
name: 'S1',
age: 10,
grades: grades1,
};
const student2: Student = {
name: 'S2',
age: 9,
grades: grades2,
};
const class1: Classroom = [student1, student2];Those notes were written while watching the tutorial videos while taking the classes from the online course Learn TypeScript on Scrimba.
Because english is not my mother language, they can contain some typos and everything written here is based on my understanding about the discussed topics and may not be 100% accurate.
If you want the full course, support the instructor by buying their course on Scrimba.
- Home
- Introduction
- Introduction to TypeScript
- The Pizza Application
- Move to TypeScript
- Defensive Coding
- Typing variables
- Typing Pizza App: part 1
- Custom types
- Typing Pizza App: part 2
- Nested Object types
- Optional Properties
- Typing Pizza App: part 3
- Array Types
- Typing Pizza App: part 4
- Literal Types
- Unions
- Typing Pizza App: part 5
- Typing Pizza App: part 6
- Typing Pizza App: part 7
- Returning Types
- Typing Pizza App: part 8
- Any Type
- Typing Pizza App: part 9
- Utility Types
- Typing Pizza App: part 10
- Generics
- Typing Pizza App: part 11