Interesting Facts About Object Types and Interfaces in TypeScript
Last Updated :
20 Mar, 2025
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,
};
Similar Reads
Interesting Facts About Classes and Object-Oriented Programming in TypeScript TypeScript extends JavaScriptâs prototype-based inheritance model while also allowing developers to use class-based object-oriented programming with modern features like access modifiers, abstract classes, and interfaces.1. Classes in TypeScript Are Syntactic Sugar TypeScript classes simplify JavaSc
3 min read
Interesting Facts About Generics in TypeScript TypeScript generics provide a powerful way to create reusable, type-safe components and functions that work with multiple types. They improve flexibility, reduce redundancy, and ensure strict type constraints without sacrificing reusability.1. Generics Make Functions FlexibleGenerics allow you to cr
3 min read
TypeScript Differences Between Type Aliases and Interfaces Type In TypeScript, both type aliases and interfaces are used to define custom types, but they have distinct differences and use cases.Type Aliases: Allow the definition of types with a custom name, applicable to primitives, unions, tuples, and more complex types. Interfaces: Primarily used for defining
4 min read
What is the difference between interface and type in TypeScript ? In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.Type in TypeScriptThe
3 min read
How to Cast Object to Interface in TypeScript ? In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU
3 min read
TypeScript - Type Annotations and Type Inference TypeScript is a superset of JavaScript that adds static typing, helping developers catch errors early. Two core features are type annotations- where developers explicitly define variable, parameter, and return types, and type inference, where TypeScript automatically deduces types based on values or
5 min read