node.js Typescript项目,键值对



所以我尝试创建一个值对类型并得到错误消息:"无法找到名称'key'。"它是如何正常工作的?

import * as websocket from "websocket";

let wsClientsList: {[key: string]: websocket.connection};    

for(key in wsClientsList){
//  ^^^ TS2304: Cannot find name 'key'.
wsClientsList[key].sendUTF(message);
console.log('send Message to: ', wsClientsList[key]);
}

您需要使用var,letconst声明名为key的变量

for(let key in wsClientsList) {
wsClientsList[key].sendUTF(message);
console.log('send Message to: ', wsClientsList[key]);
}

for infor of循环中,大多数情况下可以使用const,而不像在传统的for循环中使用增量。

最新更新