我有:
interface Intfff
{
csv: {
(url: string);
parsess(lol: string);
};
}
class Class implements Intfff
{
csv(url: string) {
}
}
var obj: Intfff = new Class();
obj.csv('sdfsdf');
obj.csv.parsess('sdf');
如何实现方法csv.parsess()? translator typeScript类上的错误:类型中缺少属性'parsess'...
帮助我理解
答案 0 :(得分:2)
为什么不尝试这个呢?
interface Intfff
{
csv(url: string): Intfff;
parsess(lol:string):string;
}
class TestClass implements Intfff
{
csv(url: string) {
return this;
}
parsess(lol: string) {
return "Hello";
}
}
var obj: Intfff = new TestClass();
obj.csv('sdfsdf').parsess('sdf');
答案 1 :(得分:2)
您无法使用类方法填充该接口。您必须将函数定义为类型any
,并在将其添加到对象之前添加缺少的成员。
interface Intfff
{
csv: {
(url: string);
parsess(lol: string);
};
}
class Test implements Intfff {
public csv : { (url: string); parsess(lol: string); };
constructor() {
var csv : any = function (url: string) {
// ...
};
csv.parsess = function (lol: string) {
// ...
};
this.csv = csv;
}
}