By Amanda Greenberry Brown


2018-10-11 14:00:51 8 Comments

To hopefully simplify my question, I have broken it down like so:

What I do understand

If this is my object:

{ 
     things: [{
         'text': 'version'
         'short': 'v',
         'order': 1,
         'id': 'A'
     },{
         'text': 'update'
         'short': 'u',
         'order': 2,
         'id': 'B'
     }]
}

...then this is my type class:

 export class Thing {
     text: string;
     short: string;
     order: number;
     id: string;
 }

What I do not understand

If this is my data:

 { things: {
         'A': {
             'text': 'version'
             'short': 'v',
             'order': 1,
             'id': 'A'
         },
         'B': {
             'text': 'update'
             'short': 'u'
             'order': 2
             'id': 'B'
         }
     }
 }

...then the type class is...?

I have tried some really creative, ineffective tries to get this to work to no avail. I'm also hoping I can make sense of what to do with this non-array array (no square-brackets). Apologies if this is a duplicate.

1 comments

@T.J. Crowder 2018-10-11 14:09:25

In that second situation, your things type would be an index type like this:

interface ThingHolder {
    [key: string]: Thing;
}

That says that the keys are strings and the values are Things.

The interface for data would be something that has a things property of that type:

interface AppropriateNameHere {
  things: ThingHolder;
}

So then, the compiler is happy with this:

const data : AppropriateNameHere = {
  things: {
    'A': {
      'text': 'version',
      'short': 'v',
      'order': 1,
      'id': 'A'
    },
    'B': {
      'text': 'update',
      'short': 'u',
      'order': 2,
      'id': 'B'
    }
  }
};

Live Example on the TypeScript playground.

@Amanda Greenberry Brown 2018-10-11 16:26:50

Excellent! Thanks very much!

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] How do I check if an array includes an object in JavaScript?

20 Answered Questions

[SOLVED] How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

52 Answered Questions

[SOLVED] How can I pretty-print JSON in a (Unix) shell script?

39 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

26 Answered Questions

[SOLVED] Convert JS object to JSON string

25 Answered Questions

[SOLVED] Safely turning a JSON string into an object

  • 2008-09-05 00:12:01
  • Matt Sheppard
  • 1122572 View
  • 1154 Score
  • 25 Answer
  • Tags:   javascript json

42 Answered Questions

[SOLVED] How do I test for an empty JavaScript object?

  • 2009-03-25 01:39:45
  • falmp
  • 1407582 View
  • 1985 Score
  • 42 Answer
  • Tags:   javascript json

20 Answered Questions

[SOLVED] Access / process (nested) objects, arrays or JSON

24 Answered Questions

[SOLVED] How can I pretty-print JSON using JavaScript?

23 Answered Questions

[SOLVED] How do you check if a variable is an array in JavaScript?

Sponsored Content