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

299
Views
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 answers
Answer question

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 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!