TYPESCRIPT

TypeScript Data Types: Syntax, Usage, and Examples

TypeScript data types form the foundation of type safety and improved developer experience in TypeScript applications. Unlike JavaScript, which is dynamically typed, TypeScript introduces a robust static type system that ensures variables, functions, and object properties follow defined rules for their values.

This early error detection enhances code quality and reduces runtime bugs.


What Are TypeScript Data Types?

TypeScript data types define the kind of values a variable can hold. This system includes primitive types such as string and number, structural types like objects and arrays, and more advanced constructs like enums and tuples.

The language’s type-checking system allows you to explicitly declare these types or infer them from assigned values. This feature strengthens your codebase by preventing unintended type coercion, which is a common issue in JavaScript.

Defining data types in TypeScript provides documentation and enforceable rules within your code editor and compiler.


Why Use Data Types in TypeScript?

By using data types in TypeScript, you make your applications safer and more maintainable. Here’s why they matter:

  • Prevent runtime errors by catching type mismatches at compile time
  • Improve code readability and team collaboration
  • Enable powerful IDE features like autocomplete, navigation, and refactoring
  • Facilitate integration with APIs and third-party libraries through clear type contracts

The TypeScript data types system ensures code behavior aligns with your expectations and simplifies debugging.


Basic TypeScript Data Types

TypeScript includes several built-in types that mirror JavaScript’s primitives and introduce additional capabilities.

String

Represents textual data.

let firstName: string = "Alice";

Number

Covers integers and floating-point numbers.

let age: number = 30;

Boolean

Holds true or false values.

let isLoggedIn: boolean = true;

Null and Undefined

These types represent absence of value.

let nothingHere: null = null;
let notAssigned: undefined = undefined;

Any

Disables type checking for a variable.

let flexible: any = 42;
flexible = "now a string";

While any offers flexibility, overusing it can defeat the purpose of using TypeScript.

Void

Used as a return type for functions that don’t return a value.

function logMessage(): void {
  console.log("No return value");
}

Never

Represents a function that never returns (e.g., it throws an error or loops forever).

function fail(): never {
  throw new Error("This always fails");
}


Complex TypeScript Data Types

In addition to primitives, TypeScript supports more advanced data types for working with collections and structured data.

Arrays

Declares a list of elements with the same type.

let numbers: number[] = [1, 2, 3];

You can also use a generic form:

let names: Array<string> = ["Alice", "Bob"];

Tuples

Store fixed-length arrays with known types at each position.

let user: [string, number] = ["Alice", 25];

Enums

Define a set of named constants.

enum Role {
  Admin,
  Editor,
  Viewer
}

let userRole: Role = Role.Editor;

Enums increase code clarity and allow readable comparisons with symbolic names.

Objects

Declare custom types using object notation.

let person: { name: string; age: number } = {
  name: "Alice",
  age: 30
};

Objects can be typed inline or defined using interfaces or type aliases.


Type Inference in TypeScript

You don’t always have to explicitly declare a type. TypeScript uses type inference to determine the type based on assigned values.

let country = "Canada"; // inferred as string

Once inferred, the variable behaves as if you had written let country: string. This feature balances safety with convenience.

However, for public APIs or function signatures, explicitly stating types improves clarity.


Union and Intersection Types

TypeScript supports advanced combinations of types:

Union Types

Allow a value to be one of several types.

let id: string | number = "ABC123";
id = 42;

Intersection Types

Merge multiple types into one.

type Contact = { email: string };
type Address = { city: string };

type User = Contact & Address;
let user: User = {
  email: "user@example.com",
  city: "Paris"
};

Union and intersection types make the TypeScript data types system more expressive.


Literal Types

You can define types that are limited to specific literal values.

let status: "success" | "error" = "success";

These are helpful when defining exact values expected in function parameters or states.


Custom Type Aliases

Create reusable names for complex types.

type Product = {
  id: number;
  name: string;
  price: number;
};

let item: Product = {
  id: 101,
  name: "Chair",
  price: 49.99
};

Custom types improve maintainability and consistency.


Data Types in Functions

Function parameters and return values can be explicitly typed.

function multiply(x: number, y: number): number {
  return x * y;
}

Typing functions helps ensure valid arguments and return types.

You can also use default and optional parameters:

function greet(name: string = "Guest"): void {
  console.log(`Hello, ${name}`);
}

function sendEmail(to: string, subject?: string): void {
  // subject is optional
}


Best Practices for TypeScript Data Types

  • Prefer explicit typing for functions, public properties, and complex objects
  • Use any only as a last resort when dealing with dynamic content
  • Favor type aliases or interfaces for object structures
  • Take advantage of type inference for simple variables
  • Combine union and literal types for flexible APIs

Good use of data types in TypeScript leads to stronger, more self-documenting code.


Summary

The TypeScript data types system provides a comprehensive way to describe and enforce variable types, function signatures, and object structures. With support for primitives, arrays, tuples, enums, objects, and advanced constructs like unions and intersections, TypeScript empowers developers to write safer, more predictable code.

Understanding and leveraging data types in TypeScript not only prevents bugs but also improves collaboration, documentation, and scalability. Mastering this system is key to writing clean and efficient TypeScript applications.

Learn to Code in TypeScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn TypeScript by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster