0

I'm creating a TypScript Declaration file for a client library with a method that returns an object having a variable string key (a record id):

getRecords(...): {
  [string]: {  // ??
    first: string,
    last: string,
    ...
  }
};

What is the correct way to specify that the key value is only known at runtime, and what is the reference for this?

getRecords returns something like:

{
  'GuT9b...pX': {
    first: 'John',
    last: 'Doe',
    ...
  },
  'aMe4T...lk': {
    first: 'Jane',
    last: 'Doe',
    ....
  }
}

(Note that this is a wrapper for an HTTP API written in PHP so I have no control over the result being an associative array (i.e. JavaScript hash), instead of an array with an .id field in each element.)

1
  • It's unclear what you're asking. Can you show a real example? Commented Jun 17, 2017 at 20:13

1 Answer 1

1

It's called indexable type:

function getRecords(key: string): { [s: string]: { first: string /*, ...*/ } } {
    return { [key]: { first: 'John' } }
}
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.