Typescript data types

Do you want to know about various Typescript data types? In this tutorial, I will explain you various data types available in TypeScript and provide examples for each.

data types in Typescript

Now, let us check out various data types in typescript. For each Typescript data type, we will also check some examples.

1. Boolean

The simplest data type in TypeScript is the Boolean. It represents true/false values.

let isCompleted: boolean = false;

2. Number

Like JavaScript, TypeScript uses floating point values for all numbers. Both integer and floating-point numbers are of type number.

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

3. String

TypeScript, like JavaScript, uses double or single quotes to surround string data.

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

4. Array

TypeScript allows you to work with arrays of values. Array types can be written in two ways: using the type of the elements followed by [], or using a generic array type Array<elemType>.

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

5. Tuple

Typescript Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same.

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

6. Enum

An enum is a way of giving more friendly names to sets of numeric values. Here is how you can use Typescript enums.

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

7. Any

The any type in Typescript is useful when you don’t want a particular value to cause type-checking errors.

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

8. Void

The Typescript void type is used for functions that do not return a value.

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

9. Null and Undefined

In TypeScript, both undefined and null have their types named undefined and null respectively.

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

10. Never

The never type represents the type of values that never occur. For instance, a function that throws an exception or one that never returns.

function error(message: string): never {
    throw new Error(message);
}

11. Object

object is a type that represents the non-primitive type, i.e., anything that is not number, string, boolean, bigint, symbol, null, or undefined.

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

Typescript int type

In TypeScript, there is no specific “int” data type as you might find in other strongly typed languages like C# or Java. TypeScript, just like JavaScript, has a single number type for all numeric values, regardless of whether they are integers or floating-point numbers.

See also  Typescript string to boolean

The number type in TypeScript is a double-precision 64-bit binary format IEEE 754 value. This means that it can represent both integer values and floating-point values. When you need an integer in TypeScript, you simply use the number type and assign it an integer value.

Here’s an example:

let integerNumber: number = 100; // Integer value

In this case, integerNumber is of the number type and is assigned an integer value. TypeScript does not differentiate between floating-point numbers and integers at the type level, which is a departure from some other languages that have distinct integer types (like int, short, long, etc.).

Typescript double type

In TypeScript, there is no specific “double” data type, as you might find in other programming languages like Java or C#. Instead, TypeScript follows JavaScript’s lead by having a single number type for all numeric values, whether they are integers, floating-point numbers, or doubles.

The number type in TypeScript (and JavaScript) is a double-precision 64-bit binary format IEEE 754 value, which can represent both floating-point numbers and integers. This means that whenever you’re dealing with numbers in TypeScript, whether they’re whole numbers or numbers with decimal points, you’re effectively working with double-precision floating-point numbers.

Here’s an example of using numbers in TypeScript:

let integer: number = 10; // Integer
let float: number = 10.5; // Floating-point number
let double: number = 10.123456789; // Double-precision floating-point number

All these variables are of the number type, but they can represent integers, floats, and double-precision numbers seamlessly. TypeScript’s handling of numbers this way simplifies the type system while providing the precision and range required for most numerical computations.

See also  ReferenceError fetch is not defined in Typescript

TypeScript Data Types

Here’s a quick summary of the TypeScript data types in a tabular format:

Data TypeDescriptionExample
booleanTrue/False valueslet isDone: boolean = false;
numberInteger and floating-point numberslet decimal: number = 6;
stringTextual datalet color: string = "blue";
arrayArray of valueslet list: number[] = [1, 2, 3];
tupleFixed-size array with known typeslet x: [string, number] = ["hello", 10];
enumFriendly names to numeric valuesenum Color {Red, Green, Blue}
anyOpt-out of type-checkinglet notSure: any = 4;
voidAbsence of having any typefunction warnUser(): void {}
null and undefinedRepresents absence of valuelet u: undefined = undefined;
neverValues that never occurfunction error(): never { throw new Error(); }
objectNon-primitive typecreate({ prop: 0 });

Conclusion

In this Typescript tutorial, we learned about the data types in Typescript with examples.

You may also like:

>