typescript - Access object values using "non-string keys" and define all as having a certain type -


not big deal wondering if it's possible access object using non-string key , define return same type whatever accessed. can figure out how both separately or can "laboriously" both together.

i guess want like:

var definitions: {[key: 'non-string']: definition} = { ... } 

or

var definitions: {[any]: definition} = { ... } 

i guess syntax doesn't exist (yet? ;) ).

define return type of object definitions have access using string key:

interface definition {     id: number;     type: string; }  var definitions: {[key: string]: definition} = {     v1: {         id: 4,         type: 'abc'     } }  definitions['v1'] 

can access definitions using non-string key not define return type:

interface definition {     id: number;     type: string; }  var definitions = {     v1: {         id: 4,         type: 'abc'     } }  definitions.v1  // rightly not show having interface of `definition` 

or laboriously:

interface definition {     id: number;     type: string; }  var definitions: {v1: definition, v2: definition, v3: definition, etc...} = {     v1: {         id: 4,         type: 'abc'     },     v2: { ... },     v3: { ... },     etc ... }  definitions.v1   // great! has explicit interface of definition  // or:  var v1: definition = {id: 4, type: 'abc'}; var definitions2 = {v1}; 

p.s. don't know correct technical terms access using keys ['v1'] or v1 is.

the indexer property of type establishes object being hash map. intentionally, means can use bracket notation access properties. thing readability because means there clear syntactic difference between hash map created using object versus other non-dynamic objects. however, there open enhancement request asking.

p.s. don't know correct technical terms access using keys ['v1'] or v1 is.

the ecmascript specification refers these “bracket notation” , “dot notation”, respectively; colloquially referred “array accessor” , “dot accessor”.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -