Skip to content

TypeScript Data Types

Types are at the core of TypeScript. They describe what kind of values variables, function parameters, and function return values can have. By specifying types, we can have the TypeScript compiler help us check code correctness.

TypeScript supports all basic JavaScript data types and adds some new types on top of them.

Primitive Types

1. Boolean

The most basic data type, with only two values: true and false.

typescript
let isDone: boolean = false;

2. Number

Like JavaScript, all numbers in TypeScript are floating-point numbers. The type of these numbers is number. Supports decimal, hexadecimal, binary, and octal literals.

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

3. String

Used to represent text data. Represented by string. Can use double quotes (") or single quotes (') to enclose strings.

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

Also supports template strings (using backticks `), which can embed expressions and span multiple lines.

typescript
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${fullName}.

I'll be ${age + 1} years old next month.`;

Composite Types

4. Array

TypeScript allows you to define arrays in two ways.

First, add [] after the element type to indicate an array composed of elements of this type:

typescript
let list: number[] = [1, 2, 3];

Second, use the array generic, Array<element type>:

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

5. Tuple

Tuple types allow you to express an array with a known number of elements and types, where the types of each element don't have to be the same.

typescript
// Declare a tuple type
let x: [string, number];
// Initialize
x = ['hello', 10]; // OK
// x = [10, 'hello']; // Error

Special Types

6. Any

Sometimes, we don't want the type checker to check a value, but want it to pass through the compilation stage directly. In this case, we can use the any type to mark these variables. The any type can be a value of any type.

typescript
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

Using any loses most of the advantages TypeScript brings, so it should be used cautiously.

7. Void

The void type is the opposite of the any type, representing no type at all. When a function has no return value, you'll typically see its return value type is void.

typescript
function warnUser(): void {
    console.log("This is my warning message");
}

8. Null and Undefined

In TypeScript, null and undefined each have their own types, called null and undefined respectively. By default, they are subtypes of all types, meaning you can assign null and undefined to number type variables.

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

When you enable the "strictNullChecks": true configuration in tsconfig.json, null and undefined can only be assigned to void and their respective types.

9. Never

The never type represents the type of values that never exist. For example, never is the return value type of function expressions or arrow function expressions that always throw exceptions or have no return value at all.

typescript
// Functions returning never must have an unreachable endpoint
function error(message: string): never {
    throw new Error(message);
}

function infiniteLoop(): never {
    while (true) {}
}

10. Object

object represents non-primitive types, that is, types other than number, string, boolean, symbol, null, or undefined. Using the object type, you can better represent APIs like Object.create.

typescript
declare function create(o: object | null): void;

create({ prop: 0 }); // OK
create(null); // OK

// create(42); // Error
// create("string"); // Error

Content is for learning and research only.