Alligator
Alligator

Reputation: 290

node.js Typescript project, key-value pair

So I try to create a value pair type and get the error message: "Cannot find name 'key'." How does it work properly?

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

Upvotes: 0

Views: 125

Answers (1)

user13258211
user13258211

Reputation:

You need to declare a variable named key using var, let or const

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

You can use const in most cases in for in and for of loops, unlike in traditional for loops with increment.

Upvotes: 1

Related Questions