I'm trying to attach my custom types into Express's Request/Response interface.
But some properties are of type any
, so I'm having really hard to override it.
ex:
// @types/expres-server-static-core
interface Request {
cookies: any;
}
// my.ts
import type {Request} from 'express';
type MyCookies = {
'apple'?: string;
'banana'?: string;
}
interface MyRequest extends Request {
cookies: MyCookies
}
// my-usecase.ts
const request: MyRequest = /* */; // MyRequest | Request
request.cookies; // still has any type because MyCookies | any will be any type
I know any
type is the top type of typescript so customized cookies
property will be ignored.
So I'm using like:
const myRequest: MyCookies = req.cookies;
It can solve this problem but I don't want to declare MyCookies
constants; req.cookies = MyCookies
is easier to use.
Is there any good way to override any
type property?
CodePudding user response:
Do not override default properties like that, because at "runtime" there is no guarantee req.cookies
will exist. So to get correct type, do type narrowing.
const handler = (req: Request, res: Response) => {
if (!req.cookies) throw new Error('unauthorised')
if (!req.cookies['cookie_name_1'] || typeof req.cookies['cookie_name_1'] !== 'string') throw new Error('unauthorised')
const cookie_name_1 = req.cookies['cookie_name_1'] // cookie_name_1 is string
}
Above one ensures existence of req.cookies
at both runtime & compilation.
CodePudding user response:
How about extending module?
declare global {
namespace Express {
export interface Request {
cookies: {
'apple': string;
'banana': string;
}
}
}
}