ken ken - 1 year ago 131
TypeScript Question

Typescript Dictionary failing to perform

I have a very simple Typescript definition for a "dictionary" which looks like this:

interface IDictionary<T> {
[key: string]: T;

I then use this Interface in another interface definition like so:

interface IGatewayInput {
queryParameters: IDictionary<string>;
body?: string;

I then have a function which receives the

doSomething( input: IGatewayInput ) {
if(input.queryParameters.oauth_token) {
// ....

but while I'd expect the dictionary to allow "queryParameters" to have any key value I instead get the following error:

error TS2339: Property 'oauth_token' does not exist on type 'IDictionary'.'

What am I doing wrong? I thought my definition would allow for any string based key value?

Answer Source

You defined an "indexable type" (typescript reference) which can be used as an array indexer:

if (input.queryParameters['oauth_token']) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download