Complex Types
Learn how to apply types to more complex data structures like arrays and objects.
StartKey Concepts
Review core concepts you need to learn to master this subject
TypeScript Type for One-dimensional Array
TypeScript Generic Type for One-Dimensional Array
TypeScript Type for Multi-dimensional Array
TypeScript Empty Array Initialization
TypeScript Tuple Type
TypeScript Tuple Type Syntax
TypeScript Tuple Type Length and Order
TypeScript Tuple Array Assignment
TypeScript Type for One-dimensional Array
TypeScript Type for One-dimensional Array
// zipcodes is an array of strings
let zipcodes: string[] = ['03255', '02134', '08002', '03063'];
// Pushing a number to zipcodes will generate an error
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.
zipcodes.push(90210);
The type annotation for a one-dimensional array in TypeScript is similar to a primitive data type, except we add a []
after the type.
Arrays
Lesson 1 of 2
- 1Our TypeScript journey has now led us to a new destination: Array-bia. Typing arrays is a little bit different than working with primitive types. This is because arrays usually contain many pieces …
- 2The TypeScript type annotation for array types is fairly straightforward: we put [] after the element type. In this code, names is an Array that can only contain strings: let names: string[] = […
- 3So far we’ve looked at string[] arrays, but we could also have arrays that only contain numbers (number[]) or booleans (boolean[]). In fact, we can make arrays of any type whatsoever. We can also …
- 5TypeScript can infer variable types from initial values and return statements. Even still, we may not know exactly what type inference to expect when dealing with arrays. For example: let examAns…
- 6Assigning types to rest parameters is similar to assigning types to arrays. Here’s a rest parameter e…
- 7Like the finest wines and cheeses, TypeScript’s tuples pair beautifully with JavaScript’s spread syntax….
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory