G
G
golentor2020-12-23 11:51:18
typescript
golentor, 2020-12-23 11:51:18

Typescript doesn't work with try-catch why?

FILE /store/index.ts

export const getters: GetterTree<RootState, RootState> = {
  isLoggedIn: state => (
    try {
      return state.authUser.id !== null
    } catch {
      return false
    }  
  )
}


how to rewrite so that there is no error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-12-23
@golentor

export const getters: GetterTree<RootState, RootState> = {
  isLoggedIn: state => {
    try {
      return state.authUser.id !== null
    } catch {
      return false
    }  
  }
}
but better
export const getters: GetterTree<RootState, RootState> = {
  isLoggedIn: state => !!state?.authUser?.id
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question