In typescript, I try to get an attribute from the http request header.
The attribute is a string (of course, it is from the header), I need to parse it into Integer
export const getUser = async (req, _res, next) => {
const userId: number = parseInt(req.headers.userid);
... other code
}
However, the vs code IDE is displaying red line under req.headers.userid and complaining:
(property) IncomingMessage.headers: IncomingHttpHeaders
Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2345)
How can I do this simple task?