I need to check the object type from the request body and then run approbiate function based on this type, I try do this in this way:
export interface SomeBodyType {
id: string,
name: string,
[etc....]
}
export const someFunction = async (req: Request, res: Response) => {
const { body } = req.body;
if (body instanceof SomeBodyType) {
//run function A
} else {
// run function B
}
}
but it not work, cause my SomeBodyType
return me error: only refers to a type, but is being used as a value here.
,
so, how can i check the type of the body
in this case?
thanks for any help!
//// EDIT:
thanks to @phn answer I create this generic function to check object types:
export const checkObjectType = <T>(body: T): boolean => {
return (body as T) !== undefined;
}
please, take a look and comment if this function is good
Aucun commentaire:
Enregistrer un commentaire