1
Problem
Good afternoon! it is possible, in typescript, to give a cast of a object
to a class I created? I need precisely to validate whether the fields exist or not, and if they are filled in correctly, then the risk of a property not existing is not a problem.
Context
I am doing an API in typescript and came across a problem: One of the parameters I receive is an object that can vary according to other fields, and each of the objects has nothing to do with the other.
After much break the head (I wanted to validate everything using decorators, but I could not find a solution), I ended up leaving him as a object
even
config: object;
Beauty! However, to validate, I thought about creating the object with the constructor (or just having a validation method, in my context does not make a difference). I started doing the following:
constructor(config: object)
{
if(config.property !== undefined && config.property > 3)
//ok
}
Which brings me back to error Property 'queryId' does not exist on type 'object'.ts(2339)
. I understand that the typescript itself limits these actions, but I really don’t know how to proceed. My idea was to cast, is this the correct approach? And if so, how do I do it?