我正在努力解决如何将JavaScript代码的成员变量转换成等价的Type Script。
在我的javascript代码中,在constructor()之后,我有:

this.theMediaItem = [];
this.theMediaItem.embedLink = '';
this.theMediaItem.username = '';

我已经尝试了下面的作为类型的等价物,按需要插入在export classconstructor()之间,但是它不喜欢'.':
theMediaItem = [];
theMediaItem.embedLink = '';
theMediaItem.username = '';

最佳答案

您需要使用下面的语法,还要确保您没有将theMediaItem定义为数组,因为从使用情况来看,您分配了它的属性:

class YourClass {
    constructor() {
       this.theMediaItem = {};
       this.theMediaItem.embedLink = '';
       this.theMediaItem.username = '';
    }
}

或者用简单的方法:
class YourClass {
    theMediaItem = { embedLink: '', username: '' },
    constructor() {
       // ...
    }
}

https://www.typescriptlang.org/docs/tutorial.html

关于javascript - 将Javascript成员变量转换为Typescript成员变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37976035/

10-11 13:39