在Announcing Typescript 1.0中,它提到了“完全类型的通用承诺类型。这对我来说是希腊语。
有人可以给我一个解释,以及它的一个例子,因为它适用于Typescript 1.0?
答案 0 :(得分:3)
问题如示例中所述。以下将失败,即TS无法编译promise库的实现(它应该编译):
interface Promise<Value> {
result: Value;
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
then<T2>(f: (v: Value) => T2): Promise<T2>;
}
class PromiseImpl<Value> implements Promise<Value> {
result: Value;
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
then<T2>(f: (v: Value) => T2): Promise<T2>;
then<T2>(f: (v: Value) => any): Promise<T2> {
return undefined;
}
}
问题与定义不同。例如以下将编译(接口定义):
interface Promise<Value> {
result: Value;
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
}
interface PromiseImpl<Value> extends Promise<Value> {
result: Value;
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
then<T2>(f: (v: Value) => T2): Promise<T2>;
then<T2>(f: (v: Value) => any): Promise<T2>;
}