0

I have the following:

let user: UserModel = getUser();

let active: Boolean = getStatus();

I tried the following condition:

if (active && user && user.claims && { Extra Condition using user.claims } ) {

}

I need active to be true and then check a condition with user.claims.

But before I need to be sure that user and user.claims are defined.

However, I am getting two errors:

Type 'UserModel' is not assignable to type 'boolean'

Type 'Claim[]' is not assignable to type 'boolean'

How can I fix this?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 1
    `Boolean` is the wrong type. It is `boolean`. Also, stop annotating local variables especially, simple ones, when you can infer the types them from their initializer. It's the poor practice and interferes with type inference and control flow analysis – Aluan Haddad Sep 16 '20 at 18:56
  • 1
    You could simplify it to `active && user?.claims?.extraCondition` – adiga Sep 16 '20 at 19:04

1 Answers1

1

user != null && user.claims != null is what you want. You can also refer this answer.

Dejazmach
  • 742
  • 8
  • 15