Typescript types

Stas Patek
2 min readApr 21, 2023

--

TypeScript is a superset of JavaScript that adds optional static typing, which can help catch programming errors before code execution. TypeScript has a variety of built-in types to allow developers to define the data types of variables, functions, and parameters in their code. In this article, we will explore some of the most commonly used types in TypeScript.

  1. Boolean Type The Boolean type in TypeScript represents a logical boolean value, which can be either true or false. It can be declared as follows:
let isDone: boolean = false;

2. Number Type The Number type in TypeScript represents both integer and floating-point numbers. It can be declared as follows:

let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;

3. String Type The String type in TypeScript represents a sequence of characters. It can be declared as follows:

let name: string = "John";
let sentence: string = `Hello, my name is ${name}.`;

4. Array Type The Array type in TypeScript represents an array of values. It can be declared using either of the following notations:

let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];

5. Tuple Type The Tuple type in TypeScript represents an array with a fixed number of elements of different types. It can be declared as follows:

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

6. Enum Type The Enum type in TypeScript allows developers to define a set of names constants. It can be declared as follows:

enum Color {Red, Green, Blue};
let c: Color = Color.Green;

7. Any Type The Any type in TypeScript represents a dynamic type that can hold any value. It can be declared as follows:

let notSure: any = 4;
notSure = "maybe a string instead";

8. Void Type The Void type in TypeScript represents the absence of a value. It can be declared as follows:

function logMessage(): void {
console.log("This is a log message.");
}

9. Null and Undefined Types The Null and Undefined types in TypeScript represent null and undefined values, respectively. They can be declared as follows:

let u: undefined = undefined;
let n: null = null;

10. Object Type The Object type in TypeScript represents a non-primitive type, i.e., anything that is not of the types described above. It can be declared as follows:

declare function create(o: object | null): void;
create({ prop: 0 });
create(null);

--

--