Setting dynamic properties with key from a private collection in TypeScript -


running in http://www.typescriptlang.org/playground ...

interface testobj {   name:string; }   class test {   test;   private mycollection:testobj[] = [];   private anycollection:any[] = [];   constructor() {     var obj:any = {};     var refobj = {name:'prop'};     obj[refobj.name] = "this works!";      var collection:any[] = [];     collection.push(refobj);     this.test[collection[0].name] = "this works!";      this.mycollection.push(refobj);     obj[this.anycollection[0].name] = "this works!?";      this.mycollection.push(refobj);     obj[this.mycollection[0].name] = "what deuce!?! this.mycollection[0].name supposed string! not?";      var newrefobj:testobj = { name: 'prop' };     this.mycollection.push(newrefobj);     obj[this.mycollection[1].name] = "this doesn't work either.";   } } 

i on last part of statement.

an index expression argument must of type 'string', 'number', 'symbol, or 'any'. (property) test.mycollection: testobj[] 

i guess might having hard time understanding "private" properties. have done crazy amounts of research have hard time understanding geek speak. can't seem find having issue presuming it's sort of noob thing.

ideally, rather show error if tried using mycollection outside of test class.

the primitive type of string string , not string. change interface accordingly:

interface testobj {   name: string; } 

string string object while string primitive type. example:

var obj: = {}; var strobj = new string("test"); // typed string var str = "test";                // typed string  obj[strobj] = "test";            // error seeing obj[str] = "test";               // ok 

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 -