Greg Gum
Greg Gum

Reputation: 37895

fully-typed generic promise typings

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?

Upvotes: 3

Views: 2086

Answers (1)

basarat
basarat

Reputation: 276269

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

Upvotes: 3

Related Questions