简体   繁体   English

打字稿重载函数

[英]Typescript Overloaded Function

I am attempting to call an overloaded function listen on a Server object.我正在尝试调用重载函数listen Server对象。 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 Server 对象具有以下定义

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 .不确定发生这种情况的真正原因,但我已经通过方法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. process.env.PORT 是字符串类型,而函数接受数字。 This should fix it:这应该解决它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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