【问题标题】:'() => Promise<T>' is not assignable to type 'Promise<T>''() => Promise<T>' 不能分配给类型 'Promise<T>'
【发布时间】:2019-03-04 18:34:53
【问题描述】:

我有一个界面:

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

【问题讨论】:

  • 在第一个代码块中,您将getChildren 作为属性;在第二个代码 sn-p 中,您将 getChildren 作为函数。

标签: typescript async-await


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    相关资源
    最近更新 更多