Open In App

Interesting Facts About Object Types and Interfaces in TypeScript

Last Updated : 20 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript enhances object types and interfaces with strong typing, extendibility, and dynamic features, making code more structured and maintainable. Mastering these concepts improves scalability, flexibility, and type safety in applications.

1. Interfaces Can Describe Functions

Interfaces in TypeScript aren't limited to objects. They can also define the structure of functions, ensuring that functions follow a specific signature with defined inputs and outputs.

JavaScript
interface GreetFunction {
    (name: string): string; 
}

const greet: GreetFunction = (name) => `Hello, ${name}!`;
console.log(greet("Alice")); // Hello, Alice!

2. Objects Can Have Optional Properties

You can mark object properties as optional using a ?. This means that the property is not required when creating an object.

JavaScript
interface User {
    name: string;
    age?: number; // `age` is optional
}

const user: User = { name: "Alice" }; 

3. Interfaces Can Be Combined

TypeScript allows merging multiple interfaces into one. This helps in reusing and extending interfaces.

JavaScript
interface Person {
    name: string;
}

interface Person {
    age: number;
}

const person: Person = {
    name: "Alice",
    age: 25, // Both `name` and `age` are required
};

4. Interfaces Can Merge Automatically

If you define the same interface multiple times, TypeScript automatically merges them into one interface. This is called declaration merging.

JavaScript
interface Car {
    brand: string;
}
interface Car {
    model: string;
}
const myCar: Car = { brand: "Tesla", model: "Model 3" }; // Works!

5. Interfaces Can Extend Classes

Interfaces can extend classes, meaning they inherit properties and methods from the class, allowing you to use them with more flexibility.

JavaScript
class Person {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
}

interface Employee extends Person {
    employeeId: number;
}

const employee: Employee = {
    name: "Alice",
    employeeId: 123,
};

6. Objects Can Use unknown for Flexible Properties

The unknown type allows you to have properties with flexible types while still enforcing type safety. Before using the value, you must check its type.

JavaScript
interface User {
    id: number;
    metadata: unknown; 
}

const user: User = {
    id: 1,
    metadata: { role: "admin" },
};

if (typeof user.metadata === "object" && user.metadata !== null) {
    console.log((user.metadata as { role: string }).role); // admin
}

7. Interfaces Can Describe Both Objects and Functions

Interfaces can describe the shape of both objects and functions. This is useful when defining function signatures.

JavaScript
interface Logger {
  (message: string): void;
}

const log: Logger = (msg) => console.log(msg);
log("Hello, TypeScript!"); 

8. Interfaces Can Extend Arrays and Tuples

Interfaces can be used to define specific structures for arrays and tuples, giving more control over their structure.

JavaScript
interface Point2D extends Array<number> {
  0: number;
  1: number;
}

const coords: Point2D = [10, 20];

9. Utility Types Can Modify Object Structures Efficiently

TypeScript provides utility types like Partial<>, Required<>, Pick<>, and Omit<> to modify objects without redefining them.

JavaScript
interface User {
  id: number;
  name: string;
  age: number;
}

type UserPreview = Pick<User, "id" | "name">; 

10. Objects Can Use Intersection Types

Intersection types allow you to combine multiple types into one, merging all their properties into a single object type.

JavaScript
type Person = { name: string };
type Employee = { employeeId: number };

const employee: Person & Employee = {
    name: "Alice",
    employeeId: 123,
};




Article Tags :

Similar Reads