简体   繁体   中英

Typescript Overloaded Function

I am attempting to call an overloaded function listen on a Server object. What seems to be happening is that the compiler is matching the wrong method to the call, even though there is a matching definition. I'm not sure how to even go about debugging this.

The Server object has the following definition

class Server extends events.EventEmitter {
        constructor(connectionListener?: (socket: Socket) => void);
        constructor(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }, connectionListener?: (socket: Socket) => void);

        listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
        listen(port?: number, hostname?: string, listeningListener?: () => void): this;
        listen(port?: number, backlog?: number, listeningListener?: () => void): this;
        listen(port?: number, listeningListener?: () => void): this;
        listen(path: string, backlog?: number, listeningListener?: () => void): this;
        listen(path: string, listeningListener?: () => void): this;
        listen(options: ListenOptions, listeningListener?: () => void): this;
        listen(handle: any, backlog?: number, listeningListener?: () => void): this;
        listen(handle: any, listeningListener?: () => void): this;

...

The following compiles and runs fine:

server.listen(8080)

The following does not compile at all:

server.listen(8080, '0.0.0.0')

Intellisense reports

No overload matches this call.
  The last overload gave the following error.
    Argument of type '"0.0.0.0"' is not assignable to parameter of type '() => void'.ts(2769)
net.d.ts(193, 9): The last overload is declared here.

and the compiler similarly complains:

node_modules/ts-node/src/index.ts:261
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
lib/app.ts(333,35): error TS2345: Argument of type '"0.0.0.0"' is not assignable to parameter of type '() => void'.

    at createTSError (node_modules/ts-node/src/index.ts:261:12)
    at getOutput (node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (node_modules/ts-node/src/index.ts:558:11)
    at Module.m._compile (node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)

Thanks in advance!

I have experienced the same issue today. Not sure about the real reason for this to happen but I have fixed it by passing arguments through the method call .

server.listen.call(server, 4000, '0.0.0.0', () => {
  console.log(server.address()) // output is { address: '0.0.0.0', family: 'IPv4', port: 4000 }
});

You have a type mismatch. process.env.PORT is of type string while the function accepts number. This should fix it:

const port = Number(process.env.PORT) || 3000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM