문제

In Announcing Typescript 1.0 it mentions "fully-typed generic promise typings. That's Greek to me.

Can someone give me an explanation of this and an example of it's use as it applies to Typescript 1.0?

도움이 되었습니까?

해결책

The issue is as mentioned in the example. The following would fail, i.e. TS could not compile an implementation of a promise library (and it should compile):

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

The issue is different from being able to define. e.g. the following would compile (an interface definition):

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>;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top