Aurelia RoutableComponentActivate实现

时间:2017-06-19 18:04:54

标签: typescript aurelia aurelia-router

我确定我忽略了一些明显的东西,但我似乎无法看到它是什么。

我非常喜欢使用Typescript的类型安全性,并且我正在尝试将其纳入我的Aurelia项目中,但我遇到了一些障碍。

我有一个LectureDetails.ts组件,如下所示:

import { autoinject } from 'aurelia-framework';
import { NavigationInstruction, RouteConfig, RoutableComponentActivate } from 'aurelia-router';
import { ILecture } from './lecture';

@autoinject()
export class LectureDetails implements RoutableComponentActivate {
  lecture: ILecture;

  constructor(private http: HttpClient){}

  activate(params: any, routeConfig: RouteConfig, navigationInstruction: NavigationInstruction): Promise<ILecture> {
    return this.http
               .fecth('/api/lectures/1', { method: 'GET' })
               .then(response => response.json())
               .then(lecture => this.lecture = lecture)
               .catch(error => console.log(error));
  }
}

问题是Typescript编译器一直在抱怨activate方法,我无法弄清楚如何更新它以使其符合规范。

Class 'LectureDetails' incorrectly implements interface 'RoutableComponentActivate'.

Types of property 'activate' are incompatible.

...

看起来打字文件期望返回类型为Promise<void> | PromiseLike<void> | IObservable | void,但我不知道如何将fetch/then方法更新为该类型。如果我删除类型,代码编译并运行就好了。

我怎样才能使这个方法编译并保持类型安全?为什么不是RoutableComponentActivate类型的Promise<T>方法的返回类型?

UPDATE:好像,如果我更新签名以返回Promise<any>,那么它将会编译。我认为实际类型在那时并不重要,因为它是框架/约定所调用的方法,因此框架不需要知道在解析时promise将返回的类型?

0 个答案:

没有答案