Estoy usando mecanografiado con NodeJS y express.
this.app.listen(port, (err: any) => { if (err) { console.log("err", err) } else { console.log(`Server is listing on port ${port}`); } });El código anterior me da un error en el código de Visual Studio y también cuando intento compilar mecanografiado. Recibo el siguiente mensaje de error cuando intento compilar el mecanografiado.
Ninguna sobrecarga coincide con esta llamada. La última sobrecarga dio el siguiente error.
El argumento de tipo '(err: any) => void' no se puede asignar al parámetro de tipo '() => void'.ts(2769)
index.d.ts(1205, 5): Aquí se declara la última sobrecarga. Cita en bloque
He probado el siguiente código: Sigo teniendo el mismo error.
this.app.listen(port, (err: any, req: express.Request, res: express.Response)Parece que no entiendo por qué recibo este error. Gracias por adelantado.
Hay cinco sobrecargas disponibles para el método de listen . Y ninguno de ellos toma la devolución de llamada como (error) => void . Consulte aquí: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/ts4.0/index.d.ts#L1110-L1115
Enlace permanente, si el enlace anterior no funciona
listen(port: number, hostname: string, backlog: number, callback?: () => void): http.Server; listen(port: number, hostname: string, callback?: () => void): http.Server; listen(port: number, callback?: () => void): http.Server; listen(callback?: () => void): http.Server; listen(path: string, callback?: () => void): http.Server; listen(handle: any, listeningListener?: () => void): http.Server;Entonces, tendrás que hacer lo siguiente:
this.app.listen(port, () => { console.log(`Server is listing on port ${port}`); });O tal vez
const server = this.app.listen(port, () => { console.log(`Server is listing on port ${port}`); }); server.on('error', e => console.error("Error", e));