【问题标题】:how to expand typescript definition for nodejs module如何扩展nodejs模块的打字稿定义
【发布时间】:2018-02-11 06:41:04
【问题描述】:

我使用 nodejs 模块“ws”并安装类型typings i dt~ws

import * as WebSocket from "ws";
function add(client:WebScoket){
  let cid = client.clientId;
}

我想扩展 WebSocket 属性为clientId,但我不知道怎么做。

我已经尝试在我的定义文件index.d.ts 中编写后续代码

declare module "ws" {
    class WebSocket {
        ip: string;
        clientId: number;
        project: string;
    }
}

但无效

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

    您是否尝试过扩展 websocket 类并使用它?

    例如:

    interface WebSocketWithCliendId extends WebSocket {
        clientId: string;
    }
    

    【讨论】:

    • 我用过这个方法,但是所有来自“ws”文件index.d.ts的定义WebSocket,都需要改成WebSocketWithCliendId。如:ws.on("connection",(client,req)=>{ 应写为:ws.on("connection",(client:WebSocketWithCliendId ,req)=>{
    猜你喜欢
    • 2020-05-19
    • 2016-12-12
    • 2016-07-04
    • 2019-06-20
    • 1970-01-01
    • 2016-11-06
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多