How to do dynamic objects in TypeScript -


is there way define dynamic object type in typescript? in following example, define type "my complex type" saying:

objects of type "my complex type" objects having "any number of properties" values of properties must of type ivalue.

// value interface interface ivalue {     prop:string }  // complex type mytype = {     field1:ivalue     field2:ivalue     .     .     .     fieldn:ivalue }  // using complex type   interface sometype {     prop:my complex type } 

yes, kind of behavior can achieved, in different way. need use typescript interface, like:

interface ivalue {     prop: string }  interface mytype {     [name: string]: ivalue; } 

that used example like:

var t: mytype = {}; t['field1'] = { prop: null }; t['field2'] = new differenttype(); // compile-time error ... var val = t['field1']; val.prop = 'my prop value'; 

you don't have create typescript class, need regular javascript object ( in case {} ) , make implement interface mytype, behaves dictionary , provide compile-time type safety.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -