2

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 Answer 1

6

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

console.log(x['question1']
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.