scrimba
Learn TypeScript
Function return types
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Function return types
AboutCommentsNotes
Function return types
Expand for more info
index.ts
run
preview
console
type UserRole = "guest" | "member" | "admin"

type User = {
username: string
role: UserRole
}

const users: User[] = [
{ username: "john_doe", role: "member" },
{ username: "jane_doe", role: "admin" },
{ username: "guest_user", role: "guest" }
];

function fetchUserDetails(username: string) {
const user = users.find(user => user.username === username)
if (!user) {
throw new Error(`User with username ${username} not found`)
}
return user
}
Console
"red"
,
/index.html
-2:57