Typescript Basic Types

We’ve mentioned typescript quite a few times here and we’re big fans of typescript here! In fact the search functionality on our site was written with Typescript + React. Read more about that in our behind the scenes guide Since Typescript is statically typed, let’s go through the basic typescript types so we can get familiar with what types can be in our tool belt.

Booleans:

const done: boolean = false;
let isComplete: boolean = true;

// will throw a compile error
isComplete = 123;

Numbers & Strings:

const decimal: number = 6;
const convertedNumber: number = Number('123');

let color: string = "blue";
color = 'red';

Tuples:

let x: [string, number];
x = ["hello", 10]; // OK

// this will throw an error as it does not fit the definition of string, number
x = [10, "hello"]; // Error

Enums

enum Color {Red, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];

console.log(colorName); // Displays 'Green' as its value is 2 above

Read more about it here

Instagram Post