Koopakiller
Koopakiller

Reputation: 2884

'() => Promise<T>' is not assignable to type 'Promise<T>'

I have an interface:

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

and an implementation of it:

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;
  }
  ...

For me it looks fine but I get an error:

Types of property 'getChildren' are incompatible.

Type '() => Promise' is not assignable to type 'Promise'.

Property 'then' is missing in type '() => Promise'.

What is wrong with my getChildren implementation?

I am using TypeScript 2.5.3.

Upvotes: 1

Views: 1617

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249476

The problem is getChildren on ITreeViewItem is not a function returning a promise, it's just a promise. You can declare it as a method, returning a Promise by adding ()

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

Upvotes: 2

Related Questions