0 of 6 problems solved0%
2 of 6

Password or Token

The in operator narrows a union of object types by checking if a property exists. If 'password' in user is true, you know you have the password-based variant.

function needsPwd(u: { password: string } | { token: string }) {
  return "password" in u;
}

Return a boolean so you can drive decisions using simple conditions.

Your Task

Decide if an account requires a password.

  • Define type PasswordUser = { username: string; password: string }.
  • Define type TokenUser = { username: string; token: string }.
  • Define type Account = PasswordUser | TokenUser.
  • Write function requiresPassword(a: Account): boolean using the in operator.
  • Do not mutate the input.