1. TypeScript Interface Practice

This would be an interface for a User with the following properties:

  • id (number)
  • username (string)
  • email (string)
  • createdAt (Date)
interface User {
  id: number;
  username: string;
  email: string;
  createdAt: Date;
}
  • Define these interfaces for a todo application:
    • Todo (id, title, completed, userId, createdAt, dueDate?)
    • Project (id, name, description, ownerId, todoIds)

2. Function Signatures

This is a function signature for createUser, which takes user data and returns a User:

function createUser(data: CreateUserData): User;

Write function signatures for:

  • getTodosByUser: takes userId, returns array of Todos
  • updateTodoStatus: takes todoId and completed status, returns updated Todo
  • deleteProject: takes projectId, returns boolean (success/fail)

3. API Response Types

This is an API response for a successful user fetch:

interface ApiResponse {
  status: number;
  success: boolean;
  data?: unknown;
  error?: string;
}

const response: ApiResponse = {
  status: 200,
  success: true,
  data: {
    id: 1,
    name: "Alex",
    email: "alex@example.com",
  },
};

Use the ApiResponse interface to create:

  • Failed user fetch (user not found)
  • Successful list of todos
  • Server error response
Tags: