Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Has callback() changed return type? Now error from eslint #1755

Open
3 tasks done
crystalfp opened this issue Mar 28, 2023 · 5 comments
Open
3 tasks done

[fix] Has callback() changed return type? Now error from eslint #1755

crystalfp opened this issue Mar 28, 2023 · 5 comments
Labels

Comments

@crystalfp
Copy link

Describe the bug

The following ts code has always worked:

import http2 from "node:http2";
import fs from "node:fs";

import Koa from "koa";

const options = {
	key:  fs.readFileSync("./cert/localhost-privkey.pem"),
	cert: fs.readFileSync("./cert/localhost-cert.pem")
};
const port = 3322;

const runApp = () => {
	console.log("Running")
}
const handleServerErrors = (error) => {
	console.log("Error", error)
}

const app = new Koa();
const server = http2
			.createSecureServer(options, app.callback()) // <-- Here
			.listen(port, () => runApp())
			.once("error", handleServerErrors);

Node.js version: 19.8.1

OS version: Windows 11 64 bits

Koa version: 2.14.1

@types/koa version: 2.13.6

Description: Wrong type for the app.callback() function.

Actual behavior

eslint complains: Promise returned in function argument where a void return was expected (@typescript-eslint/no-misused-promises) at the marked line. Is it my fault (but always compiled clearly) or the typing is wrong? That is, has the function changed and I should change the code or is it an error in @types/koa?

Expected behavior

No complains from eslint.

Code to reproduce

See above

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@crystalfp crystalfp added the bug label Mar 28, 2023
@crystalfp
Copy link
Author

crystalfp commented Mar 28, 2023

In the meantime added issue to DefinitelyTyped @types/koa module DefinitelyTyped/DefinitelyTyped#64923

@zacanger
Copy link
Contributor

This change (the one referenced in your issue on the @types repo) was incorrect. Returning a function that returns a promise isn't the same as returning a Promise. It looks like the original change mentions that issue, but the current version of the types on npm is still incorrect.

@younes-io
Copy link

@crystalfp did you try something like this

import http2 from "node:http2";
import fs from "node:fs";

import Koa from "koa";

const options = {
  key: fs.readFileSync("./cert/localhost-privkey.pem"),
  cert: fs.readFileSync("./cert/localhost-cert.pem"),
};
const port = 3322;

const runApp = () => {
  console.log("Running");
};
const handleServerErrors = (error) => {
  console.log("Error", error);
};

const app = new Koa();
const startServer = async () => {
  const server = http2.createSecureServer(options, app.callback());
  server.listen(port, () => runApp()).once("error", handleServerErrors);
};

startServer();

@crystalfp
Copy link
Author

This is exactly my code (also in my first question BTW). The problem is the typing of app.callback() not the code.

@3imed-jaberi
Copy link
Member

3imed-jaberi commented Jun 29, 2023

@crystalfp This should be fixed on the @types/koa side.

I am not sure but maybe wrapping the callback method on another anonymous function like () => { app.callback() } be work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants