Srushti Shah
Srushti Shah

Reputation: 874

Property 'roles' does not exist on type 'User' in Typescript

I am a newbie in Typescript. I have a project in meteor, react with typescript and javascript.

I have a button which can be visible if the current user is having admin role. My code is,

{Meteor.user() && Meteor.user()?.roles && ["admin"].includes(Meteor?.user()?.roles) && 
    <Button onClick={handleClick}>Go to SMS Room</Button>
}

The viewer those who do not know Meteor.user(), It will return the object of current user.

I am getting error like Property 'roles' does not exist on type 'User'

Can anyone tell me what to do and how to remove the error ?

Upvotes: 0

Views: 398

Answers (1)

Mikkel
Mikkel

Reputation: 7777

The default Meteor user collection does not include roles. Version 2 of alanning:roles implemented roles that way, which is probably what you are using.

You will need to declare a type that has the roles attribute, and then Typescript will be happy with you.

You will probably need to get the user record into a (typed) variable, let's say it's currentUser, and then refer to currentUser.roles instead of Meteor?.user()?.roles in your if statement.

Upvotes: 1

Related Questions