Categories
TypeScript Answers

How to fix the “No overload matches this call. Type ‘string’ is not assignable to type ‘Signals'” error in TypeScript?

Spread the love

Sometimes, we want to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript.

In this article, we’ll look at how to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript.

How to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript?

To fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript, we can use a type guard to filter out the values that don’t have the Signals type.

To do this, we write

enum Signals {
  SIGHUP = 1,
  SIGINT = 2,
  SIGTERM = 15,
}

Object.values(Signals)
  .filter((s): s is NodeJS.Signals => typeof s !== "number")
  .forEach((signal) => {
    process.on(signal, {
      //...
    });
  });

to get the enum values from the Signals enum with Object.values.

Then we call filter with a type guard function that filters out all the non-number values from the array returned by Object.values.

Finally, we call forEach on the array returned by filter.

Conclusion

To fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript, we can use a type guard to filter out the values that don’t have the Signals type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *