No overload matches this call. Type 'string' is not assignable to type 'Signals'

Javascriptnode.jsTypescriptSignals

Javascript Problem Overview


I am using typescript to build a microservice and handling signals as well. The code was working fine till a few days ago but recently it started throwing errors. Couldn't find a fix for the issue.

code for handling signals. It is just part of the file. src/main.ts

  enum signals {
    SIGHUP = 1,
    SIGINT = 2,
    SIGTERM = 15
  }
  const shutdown = (signal, value) => {
    logger.warn("shutdown!")
    Db.closeAll()
    process.exit(value)
  }
  Object.values(signals).forEach(signal => {
    process.on(signal, () => {
      logger.warn(`process received a ${signal} signal`)
      shutdown(signal, signals[signal])
    })
  })

When I do ts-node src/main.ts The following error throws and exits.


/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:245
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/main.ts:35:16 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string | signals' is not assignable to parameter of type 'Signals'.
      Type 'string' is not assignable to type 'Signals'.

35     process.on(signal, () => {
                  ~~~~~~

  node_modules/@types/node/base.d.ts:653:9
    653         on(event: Signals, listener: SignalsListener): this;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.

    at createTSError (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:245:12)
    at reportTSError (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:249:19)
    at getOutput (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:362:34)
    at Object.compile (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:395:32)
    at Module.m._compile (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:473:43)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:476:12)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

Any fix would be appreciated. Or If you can tell why it was working earlier just 2 days ago and not now.

Javascript Solutions


Solution 1 - Javascript

This sometimes happens when you have passed an incorrect number of arguments to an anonymous function:

    Object.keys(data).reduce((key: string) => {

    }, {}); 

will raise error:

> No overload matches this call. Overload 1 of 3

Pass it the correct number of arguments:

    Object.keys(data).reduce((acc: any, key: string) => {
    }, {});

Solution 2 - Javascript

I also had this strange issue, but I worked around it using type assertions (in my case using a string enum):

(Object.values(someEnum) as string[]).concat(otherStringArray);

Solution 3 - Javascript

Solution 1: Keep numeric enum signals
Object.values(signals)
  // numeric enum includes reverse mapping, filter numbers out and keep "SIGHUP" etc.
  .filter((s): s is NodeJS.Signals => typeof s !== "number") 
  .forEach(signal => {
    process.on(signal, ...) // works now
  })
Solution 2: Use pure signal string literal types
// these string literal items are strongly typed by built-in NodeJS.Signals type
Object.values<NodeJS.Signals>(["SIGHUP", "SIGINT", "SIGTERM"])
  .forEach(signal => {
    process.on(signal, ...) // works now
  })
Solution 3: Change to string enum (no reverse mapping)
enum signals2 {
  SIGHUP = "SIGHUP",
  SIGINT = "SIGINT",
  SIGTERM = "SIGTERM"
}

Object.values(signals2)
  .forEach(signal => {
    process.on(signal, ...) // works now
  })
Why does the error happen?

Numeric enums like signals include a reverse mapping. For example you can do the following:

const r1 = signals.SIGHUP // r1 value: 1
const r2 = signals[signals.SIGINT] // r2 value: "SIGINT"
const r3 = signals[15] // r3 value: "SIGTERM"

That is why you get (string | signals)[] back for Object.values(signals), where string stands for the enum keys and signals for the enum values.

Now, parameter signal in process.on(signal, ...) must be one of the predefined Node.JS string literal types. However we pass in string | signals item type, so TS yells at this point.

Solution 4 - Javascript

This happens when you do not assign your types properly.

Your variable data Type MUST match the passing data type.

organizationList: { id: string; name: string;  }[] = [] //if you do this way it type error
organizationList: Organization[] = [] //correct defining method 

get Org() {
    return this.list.filter((org: Organization) => org.id == this.id) //this data type must defined data type
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionM A SIDDIQUIView Question on Stackoverflow
Solution 1 - JavascriptDaniel ViglioneView Answer on Stackoverflow
Solution 2 - Javascriptkitsu.ebView Answer on Stackoverflow
Solution 3 - Javascriptford04View Answer on Stackoverflow
Solution 4 - JavascriptKasunSHView Answer on Stackoverflow