Chuck's Academy

Basic TypeScript

Data Types in TypeScript

One of the most powerful aspects of TypeScript is its type system, which allows you to define the data types that variables, functions, and other code elements must handle. Below, we will explore the basic and advanced data types that you can utilize to make your code more robust and predictable.

Primitive Types

TypeScript includes all the primitive data types found in JavaScript. These are:

  • number: for numbers, both integers and decimals.
  • string: for text.
  • boolean: for boolean values (true or false).
  • null and undefined: to represent the absence of value.

Here is an example of how to declare variables with primitive types:

typescript
"This code declares three variables: age, which is a number and has the value of 25; name, which is a string with the value Alice; and isStudent, which is a boolean with the value true."

Esta imagen muestra distintos data typesEsta imagen muestra distintos data types

Complex Types

In addition to primitive types, TypeScript offers various complex data types that allow for more sophisticated data structures:

  • Arrays: Arrays in TypeScript can be defined with homogeneous types.
  • Tuples: Tuples are arrays with a fixed number of elements, each having a specific type.
  • Enums: Enums allow you to define a set of named constants.

Arrays

To declare an array in TypeScript, you can use the type[] or Array<type> notation:

typescript
"In this example, numbers is an array of numbers, and names is an array of strings. Both use different notations but serve the same purpose."

Tuples

Tuples allow you to declare an array with a specific number and types of values:

typescript
"Here, person is a tuple that contains a string and a number, in that order. The first position is for the name, which is a string, and the second for the age, which is a number."

Enums

Enums in TypeScript are used to declare a set of clearly-named constants:

typescript
"This code defines an enum named Color with three possible values: Red, Green, and Blue. Then, the value Green is assigned to the variable favoriteColor."

Custom Types: Alias and Union

TypeScript allows you to create custom types using aliases and union types. This facilitates code readability and maintenance.

Type Aliases

Type aliases let you create a name for a complex type, simplifying its use in various parts of the code:

typescript
"Here, a type alias called UserID is created, which can be either a string or a number. Then, a number is first assigned followed by a string to the variable userId."

Union Types

Union types allow a variable to have more than one type:

typescript
"The score variable can be a number or a string, and this example initializes it first with a number and then with a string."

Conclusion

In this chapter, we have explored the data types in TypeScript, from primitive types to complex types like arrays, tuples, and enums, as well as the creation of aliases and union types. These concepts are essential to leverage TypeScript's type system and write clearer and safer code.


Ask me anything