typescript-tipos-de-datos

Understanding Types in TypeScript

  • 4 min

Types in TypeScript are the basic tool for defining the characteristics and behavior of variables and objects in the code.

Types allow us to specify what kind of values a variable can hold, what kind of arguments a function can receive, and what kind of value a function will return (among others).

The type system in TypeScript is a bit “special” compared to other languages. Some of the features that make it special are:

  • Static: Typing is checked at compile time, allowing errors to be detected before running the code.
  • Optional: Although TypeScript is a strongly typed language, typing is optional and can be progressively added to a project.
  • Structural: TypeScript uses structural typing, where two types are compatible if they have the same shape or structure.

Type Categories in TypeScript

Types in TypeScript can be divided into several main categories:

typescript-objetos-primitivos

  1. Static Types: These are the types checked at compile time. They include:

    • Primitive Types: The basic types found in almost all programming languages.
    • User Defined Types: Types that the user defines to meet specific application needs.
  2. Generic Types: Allow the definition of components that can work with different types without specifying the exact type beforehand. Generics provide a way to create reusable functions and classes.

  3. Decorators: They are a special kind of declaration that can be applied to classes, methods, properties, and parameters. Decorators allow adding metadata and modifying the behavior of classes and their members.

Static Types

Primitive Types

The common primitive types in TypeScript:

TypeDescription
numberRepresents both integer and floating-point numbers
bigintRepresents large integers
stringRepresents text strings
booleanRepresents logical values: true or false
symbolRepresents a unique and mutable value

Here are other primitive types that have special use:

TypeDescription
anyRepresents a type compatible with all others
voidRepresents the absence of a return value
undefinedRepresents a variable that has been declared but not initialized
nullRepresents the absence of a value
neverRepresents the type of value that never occurs

User Defined Types

User defined types include:

TypeDescription
literalRepresents an element that accepts a series of specific values
enumRepresents a set of named constants
classRepresents a template for creating objects with defined properties and methods
arrayRepresents a list of values
tupleRepresents a collection with a fixed number of elements
functionRepresents a function reference

Type compositions:

TypeDescription
unionRepresents a value that can be one of several specified types
intersectionRepresents a value that must satisfy all specified types

Generic Types

Generic types allow writing functions and classes that can work with any type of data. For example:

function identity<T>(arg: T): T {
    return arg;
}
Copied!

In this example, T is a generic type determined at runtime.

Decorators

Decorators are used to add metadata to classes and their members. For example, a class decorator:

function logClass(target: Function) {
    console.log(`Class: ${target.name}`);
}

@logClass
class MyClass {
    // Implementation of the class
}
Copied!

Type Hierarchy in TypeScript

The type hierarchy in TypeScript is organized so that each type is derived from or is compatible with other types in the system.

Here’s a little diagram with a simplified description of this hierarchy:

typescript-tipos

You don’t need to memorize this. But having it more or less in mind can help you understand some aspects, especially regarding type conversion and compatible units.