Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda