Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
Vue-router with Typescript and beforeEnter guard, how to use validated data?

I'm using Vue and vue-router with typescript, I have a common case of a single page for a Photo component to be shown, I have a route with a beforeEnter guard that queries my store to see if the requested photo actually exists

    {
        name: 'photo',
        path: '/photos/:id',
        meta: {requiresAuth: true},
        component: () => import('@/pages/Photo.vue'),
        beforeEnter: (to, from, next) => {
            const photos = usePhotos();
            const requestedPhoto = photos.$state.photos.findIndex(p => p.uuid === to.params.id)
            return requestedPhoto === -1 ? next({name: 'home'}) : next()
        },
    }

In my example, I'm already checking in the beforeEnter if the requested photo exists, now if all goes well, the user arrives inside the component.

In my component I'm fetching the photo again from the store using the following line of code

const photo = photoStore.photos.find(p => p.uuid === route.params.id)

Now TS will let me know that this photo could be undefined, since the find operation might return no results, however we already know from the guard step that this photo will indeed be found.

const photo = photoStore.photos.find(p => p.uuid === route.params.id)
const uuid = photo!.uuid

I could use the non-null assertion from Typescript, but ESLint doesn't like that, it's letting me know: ESLint: Forbidden non-null assertion.(@typescript-eslint/no-non-null-assertion)

So I'm wondering, what is the best practice to handle a situation like this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If photo is guaranteed to exist, this is the case for non-null assertion operator. ESLint rule can be temporarily disabled because it doesn't serve a good purpose here.

If photo is not guaranteed to exist, it's incorrect to use non-null assertion operator because this can result in runtime error. This is the case for type guard:

const photo = photoStore.photos.find(p => p.uuid === route.params.id)

if (photo) {
  const uuid = photo.uuid // undefined excluded from photo type
  ...
} else {
  ....
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!