yue*_*you 8 node.js typescript
我正在为我的后端代码库转换Typescript,但在收听http服务器时Error Event,我遇到了一个问题[ts] property syscall does not exist on error.我认为Error类型在这里是错误的,但显然Node.js不提供此回调函数的默认错误类型.任何人都可以帮我正确的错误类型?
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
})
server.listen(3000);
server.on('error', onError);
function onError(error: Error) {
// [ts] property syscall does not exist on error
if (error.syscall !== 'listen') {
throw error;
}
}
Run Code Online (Sandbox Code Playgroud)
Nat*_*end 12
尝试将error变量的类型更改Error为NodeJS.ErrnoException.此类型是扩展基本Error接口并添加syscall属性等的接口.
资料来源:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts#L401
export interface ErrnoException extends Error {
errno?: number;
code?: string;
path?: string;
syscall?: string;
stack?: string;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2502 次 |
| 最近记录: |