سؤال

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