1

a common use case for javascript objects is to use them as key-value storage... sort of like a dictionary:

var dictionary = {},
    value;
dictionary['key a'] = 99;
dictionary['key b'] = 12;
value = dictionary['key a'];  // 99

typescript intellisense goodness can be added by declaring an interface like this:

interface IIndexable<T> {
    [s: string]: T;
}

and using the interface like this:

var dictionary: IIndexable<number> = {},
    value: number;
dictionary['key a'] = 99;
dictionary['key b'] = 'test';  // compiler error: "cannot convert string to number"
var x = dictionary['key a'];   // intellisense: "x" is treated like a number instead of "any".

here's my question: is it possible to declare a stand-alone version of this interface:

interface StackOverflow {
    questions: IIndexable<number>;
}

ie without using IIndexable?

I tried doing something like this but it doesn't compile:

interface MyAttempt {
    questions: [s: string]: number;
}

1 Answer 1

5
interface MyAttempt {
    questions: { [s: string]: number; };
}
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.