Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dynamic variable reference in Typescript

Thanks ahead of time, to anyone who helps. :)

This may seem a simple answer to those who are experienced, but I have scoured the internet as well as a couple of reference books, and not found a straight forward answer to this question, so hopefully it may be of help to others as well.

I'm currently transitioning from Actionscript to Typescript, and have a reasonable amount of experience with vanilla Javascript, so to put it simply in Javascript terms, if I wanted to dynamically reference a variable, I could simply use something like this:

var myTextA = "Hello!";
var myTextB = "Goodbye!";
var textList = ["A", "B"];
console.log("I want to say " + this["myText" + textList[0]]);

The result would of course be: "I want to say Hello!"

In Typescript this does not appear to be possible with private variables in a class, and results in the following TSC error:

"Index signature of object type implicitly has an 'any' type."

As far as I can gather, typescript expects me to declare the variable type in some way, within the dynamic construct, however I cannot find any clear reference on how to do this.

For my own purposes, to put this in context, I am working on a project where I need to loop through a series of paired variables that all have the same beginning, but slightly different endings so simply putting the variables themselves in an array is not an option (Or would be a messy solution, at any rate).

For example:

var myVar1a, myVar1b, myVar2a, myVar2b etc...

So in the loop, I would want to refer to both a and b of each:

console.log(this["myVar" + (i+1) + "a");
console.log(this["myVar" + (i+1) + "b");

Any help is much appreciated!!

like image 360
wdlee Avatar asked Mar 01 '16 19:03

wdlee


People also ask

Can I create dynamic variable in JavaScript?

The simplest JavaScript method to create the dynamic variables is to create an array. In JavaScript, we can define the dynamic array without defining its length and use it as Map. We can map the value with the key using an array and also access the value using a key.

What is a dynamic variable JavaScript?

Dynamic variables compute their own values by executing statements and logical expressions. A dynamic variable assigns itself the result of a calculation or operation. The dynamic variable types are dynamic string, dynamic number, and dynamic True/False (Boolean).


2 Answers

I would suggest to go with object oriented 'typed' approach. After all this is why you probably want to use typescript and not javascript. So in typescript you would do this in the following manner. To give meaning to 'this' you must refer to it inside some class. It your case it could look like this:

class Test
{
    private myTextA = "Hello!";
    private myTextB = "Goodbye!";
    private textList = ["A", "B"];

    public callMe()
    {
        console.log("I want to say " + this["myText" + this.textList[0]]);
    }
}

console.log((new Test()).callMe());
like image 179
Amid Avatar answered Sep 30 '22 11:09

Amid


As far as I can gather, typescript expects me to declare the variable type in some way, within the dynamic construct, however I cannot find any clear reference on how to do this.

You need to specify an index signature. E.g.:

// Here you are saying that map is something that when accessed by a string returns a string
var map: {[key:string]:string} = {};  


map['myTextA'] = "Hello!";
map['myTextB'] = "Goodbye!";
var textList = ["A", "B"];
console.log("I want to say " + map["myText" + textList[0]]);
like image 26
basarat Avatar answered Sep 30 '22 11:09

basarat