2017-09-02 56 views
1

我用模块的NodeJS“WS”,并安装分型typings i dt~ws
如何扩大打字稿定义模块的NodeJS

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; 
    } 
} 

但效果欠佳

回答

0

您是否尝试过使用的WebSocket类的扩展和使用它吗?

例如:

interface WebSocketWithCliendId extends WebSocket { 
    clientId: string; 
} 
+0

我已经用这种方法,但所有的定义'WebSocket'从 “WS”'index.d.ts'的文件,需要改为'WebSocketWithCliendId'。例如: '''ws.on(“connection”,(client,req)=> {''' 应该写: '''ws.on(“connection”,(client:WebSocketWithCliendId,req) => {''' – 3tion