5 fundamental types in TypeScript

5 fundamental types in TypeScript

TypeScript is a JavaScript superset that adds static typing. Knowing and understanding the data types found in the language is crucial for effective programming, enabling better type control and error detection at the compilation stage. In this article, we will examine basic types such as numbers, strings, and boolean values and present methods for declaring variables. Below are several basic data types inherited from JavaScript and available in TypeScript:

  1. number: Represents numeric values, both integer and floating point.
  2. string: Represents sequences of characters.
  3. boolean: Represents a logical value, which can be  true or  false .
  4. any: Represents any data type. Assigning a value to the  any type avoids type-checking during compilation.
  5. object: Represents a complex object that can contain various properties and methods.

In addition, TypeScript also introduces new data types, such as:

  1. enum: Represents a set of named values.
  2. tuple: Represents an ordered sequence of elements with different types.
  3. null i undefined: Represents the special values null and undefined, which denote the absence of a value.
  4. array: Represents an array of values. In TypeScript, an array can be defined using type[] notation, such as number[] or string[].
  5. function: Represents a function. In TypeScript, functions can have a specific argument type and return type.

Tired of reading? Watch this video to see how Wojtek makes things quick and easy (available in Polish only).

View this post on Instagram

A post shared by Ragnarson (@ragnarsoncom)

When assigning a value to a newly declared variable, we do not need to specify its type, because TypeScript will infer the variable's type on its own. On the other hand, if we do not know the value of the variable at the time of declaration, TypeScript will use the type any, which accepts any other data type into its structure.

let petFavoriteFood

//After displaying console.log, we see:

let petFavoriteFood: any

The any  type should be avoided because of later possible compilation errors caused by a lack of type checking. Using the type any eliminates one of the most important benefits of using TypeScript, namely static typing. Static typing allows type errors to be detected at compile time before the code is executed. Using any eliminates this check, which can lead to errors during program execution. In addition, when you use type any, you lose the ability to autocomplete and hint. One of the advantages of TypeScript is the intelligent autocompletion and hints available in code editors.

Wanting to specify the type of a variable when declaring it, we specify the type name after a colon.

let petFavoriteFood: string

Wanting to assign a variable petFavoriteFood a value of a type other than:string, TypeScript will return a message:

let petFavoriteFood: string
petFavoriteFood = 5 Type number is not assignable to type string

If we change the  string  type to  number  the compiler will not return an error.

let petFavoriteFood: number
petFavoriteFood = 5 

These are just a few of the basic data types available in TypeScript. TypeScript also has many more advanced functions and types that allow you to define data types more precisely in your code. The other types will be presented in the following articles. Taking a look at the data types available in TypeScript and using them consciously will contribute to creating more reliable and scalable applications.