Key Concepts

Review core concepts you need to learn to master this subject

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.

Lesson 1 of 2
  1. 1
    Our 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 …
  2. 2
    The 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[] = […
  3. 3
    So 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 …
  4. 4
    So far we’ve looked at arrays with all elements of the same type. But, as we know, JavaScript arrays are flexible and can have elements of different types. With TypeScript, we can also define array…
  5. 5
    TypeScript 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…
  6. 6
    Assigning types to rest parameters is similar to assigning types to arrays. Here’s a rest parameter e…
  7. 7
    Like the finest wines and cheeses, TypeScript’s tuples pair beautifully with JavaScript’s spread syntax….
  8. 8
    Beautiful! Array types may seem like a monster topic, but now you’ve got it tamed. You learned: * The type annotations for arrays. * What tuples are, and how to do their type annotations. * How typ…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo