2
votes

I'm looking to make an interface for an object that has strings for keys mapping to string values. This seems like a fairly straightforward thing to do, but I haven't been able to

Error is shown here

I've checked out Is it possible to define an object containing objects? and Enforcing the type of the indexed members of a Typescript object? but I couldn't access the values correctly.

Here's the code

interface ISomeQuestions { 
    [key:string] : string; 
 }
 var x:ISomeQuestions = {
    "question1": "",
    "question2": "",
    "a": ""
};

console.log(x.question1);

The last line reflects what I am trying to accomplish

1

1 Answers

6
votes

This is by design. Since it only has the index signature that is what you need to use.

console.log(x['question1']