TypeScript Types

Last Updated : 5 Apr 2026
TypeScript Types

The TypeScript language supports different types of values. It adds optional static types to JavaScript that help developers catch type errors during development. TypeScript is useful for programmers who want type safety and structured programming in JavaScript. The type system checks types at compile time which helps us to catch errors before the code runs. It ensures that the code behaves as expected. TypeScript provides an optional static type system that allows developers to specify types where needed.

Types in TypeScript

TypeScript inherits built-in types from JavaScript and also provides additional types which are as follows:

1. Primitive Types:

Primitive types contain simple and immutable values that can be directly assigned to variables. We have discussed all the primitive data type in TypeScript below:

  • number

Like JavaScript, all the numbers in TypeScript are stored as floating-point values. These numeric values are treated like a number data type. The numeric data type can be used to represent both integers and fractions. TypeScript also supports Binary (Base 2), Octal (Base 8), Decimal (Base 10), and Hexadecimal (Base 16) literals.

Syntax:

Example:

Output:

12
14287
255
57
  • string

We use the string data type to represent the text in TypeScript. String type works with textual data. We include string literals in TypeScript by enclosing them in single or double quotation marks. It also represents a sequence of Unicode characters. Template strings use backticks (`) with ${expression}.

Syntax:

Example:

Output:

Rohan works in the IT department.
Rohan works in the IT department.
  • boolean

The string and numeric data types can have an unlimited number of different values, whereas the boolean data type can hold only two possible values, they are "true" and "false".

Syntax:

Example:

  • null

It represents a variable explicitly set to have no value. Much like the void, it is not very useful on its own. The null type accepts only one value, which is null. The null keyword is used to define the null type in TypeScript, but it is not useful because we can only assign a null value to it.

Example:

  • undefined

undefined represents variables that have not been assigned a value. The undefined type has only one value, which is undefined.

Example:

  • symbol:

In TypeScript, the symbol is one of the primitive data types. These are unique values and we often use them as identifiers for object properties to prevent naming collisions. Each symbol is unique, unlike the other primitive types like string, number or boolean. These are created using the Symbol() function.

Example:

Output:

Alice
123 
false
  • bigint:

It is a primitive type supported by TypeScript and JavaScript as a way to accommodate integers of any size. It supports arbitrarily large integers with exact precision.

Example:

Output:

Sum: 12354686100489308881n
Product: 18014398509481982n
Keyword NameType of KeywordDescription
numbernumberThe number data type represents the floating-point numbers and integers.
booleanbooleanIt illustrates the logical values: true or false.
stringstringIt represents textual data.
nullnullWhen we deliberately represent the absence of any object value, we use the null keyword.
undefinedundefinedUninitialized values are represented by it.
symbolsymbolA unique immutable value is represented by symbol.
bigintbigintIt represents the integer with arbitrary precision.

2. Object Types:

A TypeScript object is a structured collection of key-value pairs where keys are strings or symbols and values can be of any type. It includes primitives, arrays, functions or other objects. It helps us to organize and safely manipulate data.

  • Object: It represents a structured grouping of key-value pairs. In object the keys are strings or symbols and the values may be of any data type, such as primitives, array functions or other objects.  It allows us to organize storage and type-safe manipulation of data.

Example:

Output:

Hello, Rahul Kushwaha!
  • Array

An array is a collection of elements of the same data type. In TypeScript, unlike JavaScript, arrays are typed which means all elements must be of the same type. An array can be written in two ways:

1. Using the element type followed by []:

Output:

[1, 3, 5]

2. Using a generic array type:

Output:

[1, 3, 5]
  • Tuple

TypeScript data type tuple helps us to define an array with a fixed number of elements, where each element can have a specific type. It allows us to express an array where the type of a fixed number of elements is known, but they are not the same. For example, if we want to represent a value as a pair of a number and a string, then it can be written as:

Example:

Output:

[ 'Alice', 25 ]
Alice
25
Alice 25
  • Interface

An Interface is a structure that acts as a contract in our application. It defines the syntax that classes must follow which means any class that implements an interface is required to implement all its members. It cannot be instantiated but can be implemented by the class that implements it. The TypeScript compiler uses interfaces for type-checking, which is also known as "duck typing" or "structural subtyping."

Example:

Output:

6
  • Class

Classes are used to create reusable components and act as a template for creating objects. It is a logical entity that stores variables and functions to perform operations. TypeScript gets support for classes from ES6. A class contains implementation, but an interface does not contain implementation.

Example:

Output:

1 : John
  • Enums

Enums define a set of named constants. TypeScript provides both string-based and numeric-based enums. By default, enums begin numbering their members starting from 0, but we can also change this by manually setting the value to one of its elements.

Example:

Output:

1
  • Functions

A function is a logical block of code that is utilized to organize the program. Like JavaScript, TypeScript can also be used to create functions, either as a named function or as an anonymous function. Functions ensure that our program is readable, maintainable and reusable. A function declaration has the function name, return type and parameters.

Example:

Output:

8 
10
It represents a set of named constants which allow for a collection of related values.
Type NameDescription
ObjectThis represents any value that is not a primitive like string, number, Boolean, symbol, bigint, undefined or null.
ArrayIt is used to represent a collection of elements of a specific type.
TupleIt is an array with a fixed number of elements. Each element has a specific type.
InterfaceWe can describe method signatures, function types, class contracts, the shape of an object, specifying property names and types with the help of an interface.
ClassA class in TypeScript is a blueprint which allows us to create objects with specific properties and methods.
Enum
FunctionThe Function type represents any callable function, but its use is discouraged in favor of more precise function signatures.

Advanced Types

TypeScript has advanced types that provide additional capabilities for complex type definitions:

  • Union Types:

In TypeScript, a union type allows a variable to hold values of multiple types. We define it by making use of the | (pipe) symbol which makes our code flexible while still enforcing type safety.

Example:

Output:

Hello
42 
  • Intersection types:

In TypeScript, with the help of intersection types, we can combine multiple types into a single type. All the value from combined type must be included by this type. The `&` operator is used to define intersection types which makes them useful when a value must satisfy multiple type requirements at the same time.

Example:

Output:

{ name: 'Alice', age: 30, employeeId: 1234, department: 'Engineering' }
  • Literal type: The exact values for variables, function parameters or properties can be defined by literal type. It enhances type safety by ensuring variables can only hold predefined values.

Types of literal types:

1. String Literal types: Only a specific set of string values can be accepted by string literal types.

Example:

Output:

Up

2. Number Literal Types: It work exactly like string literals but for numeric values.

Example:

Output:

Error: Type '4' is not assignable to type '1 | 2 | 3'

3. Boolean Literal Types: Since a boolean has only two possible values, it can be true or false as specific types. It is rare on its own but useful in Discriminated Unions.

Example:

Output:

{ isValid: true, reason: null }
{ isValid: false, reason: 'Invalid input' }
  • Mapped Types:

Mapped types allow us to make new types by converting the properties of existent types. Mapped types help with modifications such as making properties optional, read-only, or changing their types. They can lessen code replication and are especially useful for making differences of types without directly redefining them.

Example:

Output:

{ id: 1 }
Type nameDescription
Union TypesIt allows a variable to hold any one of several specified types. It is defined utilizing the | pipe operator.
Intersection TypesIt combines multiple types into a single type. It requires a value to satisfy all included types. It is defined utilizing the & operator.
Literal TypesIt enables exact value types which allow variables to be assigned specific values only.
Mapped TypesIt creates a new type by transforming properties of an existing type according to a specified rule.
  • Void

The void is the return type of the functions that do not return any type of value. It is utilized to indicate that a function does not return any value. A variable of type void is rarely used because we can only assign undefined or null to it.

Example:

Output:

Hello, Alice!
  • any

In TypeScript, the any type is used when working with third-party code or dynamic data, where the exact type of a variable is unknown. It is useful in this case because it helps us to enable or disable type checking during compilation. It is the "super type" of all data types in TypeScript.

Example:

Output:

Hello, Any!
 5

Conclusion:

TypeScript adds a level of type safety to JavaScript programs which enables developers to identify potential problems within their code before the programs are executed. Typescript includes many different types, such as primitive data types, unions, interfaces, generics, tuples, and enumerated types. It is possible for developers to specify how their data is structured and behaves, specify contracts for the way that they expect their code to be used and clarify their intentions with regard to the use of the types defined within TypeScript.

Improved tooling, improved readability of code, easier refactoring, and fewer runtime errors all contribute to building scalable, maintainable and modern JavaScript applications by using typed TypeScript code.