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

TypeScript error #4

Open
onosendi opened this issue Jan 30, 2022 · 9 comments
Open

TypeScript error #4

onosendi opened this issue Jan 30, 2022 · 9 comments

Comments

@onosendi
Copy link

onosendi commented Jan 30, 2022

With

const closeListeners = closeWithGrace(
  { delay: 500 },
  async ({ err }) => {
    if (err) {
      fastify.log.error(err);
    }
    await fastify.close();
  },
);

I get

1. Argument of type '({ err }: { err: any; }) => Promise<void>' is not assignable to parameter of type 'CloseWithGraceAsyncCallback | CloseWithGraceCallback | undefined'.
     Type '({ err }: { err: any; }) => Promise<void>' is not assignable to type 'CloseWithGraceAsyncCallback'.
       Types of parameters '__0' and 'options' are incompatible.
         Type '{ err?: Error | undefined; signal?: Signals | undefined; manual?: boolean | undefined; }' is not assignable to type '{ err: any; }'.
           Property 'err' is optional in type '{ err?: Error | undefined; signal?: Signals | undefined; manual?: boolean | undefined; }' but required in type '{ err: any; }'.
2. Binding element 'err' implicitly has an 'any' type.

This, fixes it

const closeListeners = closeWithGrace(
  { delay: 500 },
  async ({ err }: { err?: Error }) => {
    if (err) {
      fastify.log.error(err);
    }
    await fastify.close();
  },
);
@mcollina
Copy link
Owner

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@gkampitakis
Copy link
Contributor

gkampitakis commented Jan 30, 2022

I am not convinced this is an issue with the library. The fix should be in your code where you explicitly set the type. Might be wrong of course 🤔

Another example that works as well.

import closeWithGrace, { CloseWithGraceAsyncCallback } from './index';

const callback: CloseWithGraceAsyncCallback =
  async ({ err }) => {
    if (err) {

    }
  };

const closeListeners = closeWithGrace(
  { delay: 500 },
  callback
);

@onosendi
Copy link
Author

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

When I have time I'll take a look at it.

@segevfiner
Copy link

I think adding an as closeWithGrace.CloseWithGraceAsyncCallback after the inline function expression will also work. I think the cause for this is TypeScript being unable to infer the type of the callback function due to the overload so it just infers it as any.

const closeListeners = closeWithGrace(
  { delay: 500 },
  async ({ err }) => {
    if (err) {
      fastify.log.error(err);
    }
    await fastify.close();
  } as closeWithGrace.CloseWithGraceAsyncCallback,
);

@mckelveygreg
Copy link

If you are the latest typescript and don't want to cast, here is a good excuse to use the satisfies keyword!

const closeListeners = closeWithGrace({ delay: 500 }, async function ({ err }) {
  if (err) {
    app.log.error(err)
  }
  await app.close()
} satisfies CloseWithGraceAsyncCallback)

@mcollina
Copy link
Owner

A PR to fix this would be amazing

@StepanMynarik
Copy link

StepanMynarik commented Oct 23, 2023

Imported as import * as closeWithGrace from "close-with-grace"; in my TS file.

Runtime I get an error: "closeWithGrace is not a function"

@kibertoad
Copy link

@StepanMynarik would you be open to send a PR with fix?

@StepanMynarik
Copy link

@kibertoad
Sorry, already using Graceful Server as it better suits my use case.

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

No branches or pull requests

7 participants