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

300
Visualizações
Adding types to a const in Typescript/Javascript

Suppose I have this variable declaration, I wish to add type string to typecheck that these variables are string objects

How would I go about doing this

const { someID, someName, someAPIenvironment } = useParams();

Here is what I tried

const {(someID:string), (someName:string), (someAPIenvironment:string)} = useParams();

Am I correct with my implementation?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If useParams doesn't have a useful type signature, you can cast it to your own.

interface IParams {
    someID: string,
    someName: string,
    someAPIenvironment: string
}

const { someID, someName, someAPIenvironment } = useParams() as IParams;

Note: this gives you no type-safety as this won't perform any type checking at run-time. If you want to be sure they are strings, you will need to use typeof(), eg.

typeof(someID) === 'string' && typeof(someName) === 'string' && typeof(someAPIenvironment) === 'string'

There are also some useful schema validation libraries out there, such as yup, where you can compare objects to a pre-defined schema.

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