const express = require('express');
import routes from './src/routes/index';
const resHelper = require('reshelper');
const app = express();
app.use(resHelper);
// API Routes
app.use('/destination', routes);
app.get('/', (req : any, res: any, next : any) => {
return res.data({foo: 'bar'});
})
// Setup PORT
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
code above is my index.ts,
and then i try to apply res.data (resHelper library) in my /destination
routes,
but its doenst work and i got this error :
Property 'data' does not exist on type 'Response<any, Record<string, any>, number>.
import express from 'express';
const router = express.Router();
// const resHelper = require('reshelper');
// router.use(resHelper);
router.get('/get-all', async (req, res) => {
return res.data({foo: 'bar'});
});
export default router;
srcroutesindex.ts
And i’ve try to declare global types for response in my express.d.ts like this, but I still get the error like above when i try to run
import 'express';
declare namespace Express {
export interface Request {
data: (code: number, success:boolean, data: any) => void;
}
}
New contributor
Laksamana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.