通用接口
声明通用接口
interface IResult<T> {
wasSuccessfull: boolean;
error: T;
}
var result: IResult<string> = ....
var error: string = result.error;
具有多个类型参数的通用接口
interface IRunnable<T, U> {
run(input: T): U;
}
var runnable: IRunnable<string, number> = ...
var input: string;
var result: number = runnable.run(input);
实现通用接口
interface IResult<T>{
wasSuccessfull: boolean;
error: T;
clone(): IResult<T>;
}
使用泛型类实现它:
class Result<T> implements IResult<T> {
constructor(public result: boolean, public error: T) {
}
public clone(): IResult<T> {
return new Result<T>(this.result, this.error);
}
}
使用非泛型类实现它:
class StringResult implements IResult<string> {
constructor(public result: boolean, public error: string) {
}
public clone(): IResult<string> {
return new StringResult(this.result, this.error);
}
}