我在OO.ts文件中有以下代码:
import * as $ from "jquery";
interface ValidatableInterface {
isValid() : boolean;
}
class User implements ValidatableInterface {
/*public name: string;
public password: string;*/
constructor(public name:string, public password:string) {
}
getString() {
return `${this.name} -> ${this.password}`;
}
isValid() : boolean {
return true;
}
}
class GenericRepository<T extends ValidatableInterface> {
private _url : string;
constructor(url : string) {
this._url = url;
}
public getAsync() {
return new Promise((resolve: (entities: T[]) => void, reject) => {
$.ajax({
url: this._url,
type: "GET",
dataType: "json",
success: (data) => {
let list : T[];
let items = <T[]>data.items;
for(let i = 0; i < items.length; i++) {
if(<T><any>items[i].isValid()) {
list.push(items[i]);
}
}
resolve(list);
},
error: (e) => {
reject(e);
}
})
})
}
}
export {
User,
GenericRepository
}
&#13;
在app.ts中,我有以下代码:
import { User, GenericRepository } from './OO';
let userRepository = new GenericRepository<User>('./shared/users.json');
userRepository.getAsync()
.then((users: User[])=> {
console.log(`User Repository =>`);
/*
Not working either
users.forEach(function(user) {
console.log(user.getString());
});*/
for(let i = 0; i < users.length; i++) {
console.log(users[i].getString());
}
});
&#13;
当我尝试迭代用户数组(用户类数组)时,我看到的错误是Uncaught(在promise中)TypeError:e [t] .getString不是一个函数而在forEach中我看到的错误是未捕获(承诺)TypeError:e.getString不是函数。
我无法进行类型转换:我现在得到的错误是Uncaught TypeError:n [r] .isValid不是函数
任何人都可以解释这个错误是什么。
提前致谢