在 Typescript 中创建动态变量引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35731610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:19:28  来源:igfitidea点击:

Create a dynamic variable reference in Typescript

javascripttypescriptdynamic-variablesvariable-names

提问by wdlee

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:

我目前正在从 Actionscript 过渡到 Typescript,并且对 vanilla Javascript 有相当多的经验,所以简单地用 Javascript 术语来说,如果我想动态引用一个变量,我可以简单地使用这样的东西:

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 variablesin a class, and results in the following TSC error:

在 Typescript 中,这对于类中的私有变量似乎是不可能的,并导致以下 TSC 错误:

"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:

所以在循环中,我想同时引用 a 和 b:

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

Any help is much appreciated!!

任何帮助深表感谢!!

回答by Amid

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:

我建议采用面向对象的“类型化”方法。毕竟这就是您可能想要使用 typescript 而不是 javascript 的原因。因此,在打字稿中,您可以按以下方式执行此操作。要赋予“这个”含义,您必须在某个类中引用它。你的情况可能是这样的:

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

回答by basarat

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 indexsignature. E.g.:

您需要指定一个index签名。例如:

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