首页 文章

'() => Promise<T>'不能分配给'Promise<T>'类型

提问于
浏览
1

我有一个界面:

export interface ITreeViewItem {
    getChildren: Promise<ITreeViewItem[]>;
    ...

并实施它:

export class MyClass implements ITreeViewItem {

  public async getChildren(): Promise<ITreeViewItem[]> {
    let result = await this._fileSystemService.getContents(this.fullPath);
    let items = result.map(x => {
      let y: ITreeViewItem = null;
      return y;
    });
    return items;
  }
  ...

对我来说它看起来很好,但我收到一个错误:

属性'getChildren'的类型不兼容 . 类型'()=> Promise'不能分配给'Promise'类型 . 类型'()=> Promise'中缺少属性'then' .

我的 getChildren 实施有什么问题?

我正在使用TypeScript 2.5.3 .

1 回答

  • 2

    问题是 getChildren on ITreeViewItem 不是返回承诺的函数,它只是一个承诺 . 您可以将其声明为方法,通过添加 () 返回Promise

    export interface ITreeViewItem {
        getChildren() : Promise<ITreeViewItem[]>;
    }
    

相关问题