Reputation: 1584
I have below object in typescript
let peers:any = {
key:{initiator:isInitiator1,config: configuration1,stream: localStream1},
key:{initiator:isInitiator2, config: configuration2, stream: localStream2},
key:{initiator:isInitiator2, config: configuration2, stream: localStream2}};
what is the best possible way to declare above variable with typescript without "any" ?
I need to access each object like this peers[connUserSocketId]
Upvotes: 0
Views: 54
Reputation: 12092
I think the simplest is to define an interface that describes the structure. You can change to class if you will have also additional methods to process the data.
Here is a sample interface based on the provided sample if I understood your structure correctly. You want to store multiple of the same record by some key.
interface Peers {
[key: string]: {
initiator: boolean,
config: any/* Type of config */,
stream: any /* Type of stream */
}
}
Then you define real values like this:
const peers: Peers = {
key1: { initiator: true, config: {}, stream: {} },
key2: { initiator: false, config: {}, stream: {} }
}
Upvotes: 2