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?
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.