修改 - 更新代码以反映评论
我将一些旧代码从TS 2.2.1升级到TS 2.5.2,并遇到了编译问题。 尝试编译以下代码时:
export interface IFuture<TResult> {
done(callback: (result: TResult) => void): IFuture<TResult>;
}
export interface IVoidFuture extends IFuture<void | any> {
done(callback: (result?: void) => void): IVoidFuture;
}
export class Future<TResult> implements IFuture<TResult>{
public static createResolved<TResult>(result?: TResult): IFuture<TResult> {
let asyncResult: Future<TResult> = new Future<TResult>();
return asyncResult;
}
public done(callback: (result: TResult) => void): Future<TResult> {
return this;
}
}
export class VoidFuture extends Future<void> implements IVoidFuture {
private static _resolvedFuture: IVoidFuture;
public static createResolved(result?: void): IFuture<void> {
if (!VoidFuture._resolvedFuture) {
VoidFuture._resolvedFuture = Future.createResolved<void>();
}
return VoidFuture._resolvedFuture;
}
public done(callback: (result?: void) => void): IVoidFuture {
return super.done(callback);
}
}
我收到以下错误:
file.ts(20,14): error TS2417: Class static side 'typeof VoidFuture' incorrectly extends base class static side 'typeof Future'.
Types of property 'createResolved' are incompatible.
Type '(result?: void) => IVoidFuture' is not assignable to type '<TResult>(result?: TResult) => IFuture<TResult>'.
Types of parameters 'result' and 'result' are incompatible.
Type 'TResult' is not assignable to type 'void'.
答案 0 :(得分:0)
VoidFuture类应该在编写时实现接口。未来与预期的未来不符。也许你想要未来?
export interface IFuture<TResult> {
done(callback: (result: TResult) => void): IFuture<TResult>;
}
export interface IVoidFuture extends IFuture<void | any> {
done(callback: (result?: void) => void): IVoidFuture;
}
export class Future<TResult> {
public static createResolved<TResult>(result?: TResult): IFuture<TResult> {
let asyncResult: Future<TResult> = new Future<TResult>();
return asyncResult;
}
public done(callback: (result: TResult) => void): Future<TResult> {
return this;
}
}
export class VoidFuture implements Future<TResult> {//needs to match interface definition
private static _resolvedFuture: IVoidFuture;
public static createResolved(result?: void): IVoidFuture {
if (!VoidFuture._resolvedFuture) {
VoidFuture._resolvedFuture = Future.createResolved<void>();
}
return VoidFuture._resolvedFuture;
}
public done(callback: (result?: void) => void): IVoidFuture {
return super.done(callback);
}
}