cf.completefrontend

Write the codeMedium

Why doesn't ROLES.includes(value) compile here, and how do you write isRole(value) so it narrows?

ROLES is the list of roles, kept as const so that Role comes from it. Write isRole so it compiles, returns whether the string is one of the roles, and narrows the string to Role when it is.

Write this
const ROLES = ["admin", "editor", "viewer"] as const;type Role = (typeof ROLES)[number]; function isRole(value: string) {  return ROLES.includes(value);} // const fromUrl: string = new URLSearchParams(location.search).get("role") ?? "";// if (isRole(fromUrl)) { /* fromUrl is a Role here */ }

Solve it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…