When I read an unknown variable, eg: req.body
or JSON.parse()
and know it is formatted in a certain way, eg:
type MyDataType = {
key1: string,
key2: Array<SomeOtherComplexDataType>
};
how can I convert it, so that the following works:
function fn(x: MyDataType) {}
function (req, res) {
res.send(
fn(req.body)
);
}
It keeps failing telling me that:
req.body is mixed. This type is incompatible with object type MyDataType
.
I assume this has something to do with Dynamic Type Tests but figure out how...
One way I can get this to work is by iterating through the body and copying every result over, eg:
I guess, technically this is correct - you are refining every single value and only putting in what you know to be true.
Is this the appropriate way of handling these kinds of cases?