Create a dynamic variable reference in Typescript

岁酱吖の 提交于 2021-02-07 03:33:47

问题


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!!


回答1:


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());



回答2:


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]]);


来源:https://stackoverflow.com/questions/35731610/create-a-dynamic-variable-reference-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!