美好的一天.我是使用VSCode的Type Script的新手.
获得以下错误:
错误TS2322:类型'()=>字符串'不能分配给'string'类型.
错误TS2322:类型'()=>数字'不能分配给'number'类型.
代码:
DTO.ts
interface DTO { getId(): number; getValue(): string; } export = DTO;
LinkedObject.ts
class LinkedObject { public value: string = "Not Set"; public id: number = 0; constructor(value?: string, id?: number) { this.value = value; this.id = id; } } export = LinkedObject;
我试图LinkedObject
使用上面提到的接口方法实例化类:
TravelClientFormPopulator.ts
class TravelClientFormPopulator { public populateComboBoxUsingDTOs(dataObjects: Array, comboBoxID: string): void { // Get the combo box var selectElement = document.getElementById(comboBoxID); // Reset the options selectElement.options.length = 0; var linkedObjectsToAdd: LinkedObject[] = new Array (); var defaultLinkedObject: LinkedObject = new LinkedObject("Not Selected", 0); linkedObjectsToAdd.push(defaultLinkedObject); for (var i = 0; i < dataObjects.length; i++) { var value: string = dataObjects[i].getValue; // Error here var id: number = dataObjects[i].getId; // And here var linkedObject: LinkedObject = new LinkedObject(value, id); } } }
任何帮助将受到高度赞赏.
你错过了括号:
var value: string = dataObjects[i].getValue(); var id: number = dataObjects[i].getId();
For what it worth, if anyone has this problem only in VSCode, just restart VSCode and it should fix it. Sometimes, Intellisense seems to mess up with imports or types.
与打字稿有关:'RegExpMatchArray'类型的参数不能分配给'string'类型的参数