0

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]

1 Answer 1

2

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: {} } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.