When building APIs, it’s helpful to type your responses so you always know what shape of data your backend will return. This makes your code safer and easier to use.

Here’s a simple example for a successful user response:

interface User {
  id: number;
  name: string;
  email: string;
  createdAt: Date;
}

interface UserResponse {
  success: true;
  data: User;
}

const userResponse: UserResponse = {
  success: true,
  data: {
    id: 1,
    name: "Sarah",
    email: "sarah@example.com",
    createdAt: new Date(),
  },
};


Tags: