Explore Library
Code QuizAdvanced

Partial and Utility Types Bug

Spot why assigning a Partial-typed object back to a full type fails.

Codetypescript
enum Role {
  Admin,
  User,
  Guest
}

type User = {
  id: number;
  name: string;
  role: Role;
};

function createUser(data: Partial<User>): User {
  return {
    id: data.id,
    name: data.name,
    role: data.role
  };
}

Why does this code fail to compile?