TypeScript Type System

You may be asking yourself what is a type system? A type is an easy way to refer to the different properties and functions that a value has. What this means is TypeScript allows us to define annotations that describe what data type our variables are expected to be, what our functions are expecting as input and what they will return after execution, and what any other value in our code is expected to be.

In TypeScript There are two forms of types that we can define:

  • Primitive types (JavaScript data types):
    • number
    • boolean
    • void
    • undefined
    • string
    • symbol
    • null
  • Object types (types that we define):
    • functions
    • arrays
    • classes
    • objects

So you may be asking yourself why do we care about types? Types are used by the TypeScript compiler to analyze our code for errors. Types also allow other engineers to understand what values are flowing around our codebase. Although it is not a requirement for the reasons mentioned when we write TypeScript code for good coding standards we should define types everywhere!

Examples of types

const today = new Date();

today.getMonth();

const person = {
  age: 20
};

class Color {
}

const red = new Color();
This page was last updated: 2021-12-08 Wed 00:14:55. Source