TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Type aliases requiring type parameters

Open danielearwicker opened this issue 11 years ago • 34 comments

Sometimes we need to capture a union of types that may be a mixture of primitives, functions, objects, etc. but we want to leave it parameterised (just as we already can with interfaces).

Example

A simple non-recursive example:

type Source<T> = T | (() => T);

Here, a source can either be a plain value or a nullary function that obtains a value.

We can provide uniform access to such sources:

function unwrap<T>(p: Source<T>) {
    return (typeof p === "function") ? p() : p;
}

And then we can specify model interfaces where we we leave open the nature of the source but we tie down the value types:

interface Person {
    name: Source<string>;
    age: Source<number>;
}

e.g. name is a constant, but age depends on when you ask:

var p: Person = {
    name: "John Lennon",
    age: () => ageFromDOB(1940, 10, 9),
}

But we can treat them identically in consuming code:

var n = unwrap(p.name), a = unwrap(p.age);

NB. The above is already possible with union types alone, but the interface Person has to repeat the pattern:

interface Person {
    name: string | (() => string);
    age: number | (() => number);
}

Not so bad for a simple example, but the pattern for a value source might evolve to get more complex and then you have a lot of fiddly updating to do because you "Did Repeat Yourself".

Recursion

The more flexible recursive version:

type Source<T> = T | (() => Source<T>);

A source can either be a plain value or a nullary function that obtains a source (which may be a plain value terminating recursion, or a nullary function that... and so on).

We can again provide uniform access to such sources, either with runtime recursion (risky until tail-call optimisation is widespread):

function unwrap<T>(p: Source<T>) {
    return (typeof p === "function") ? unwrap(p()) : p;
}

Or with a loop:

function unwrap<T>(p: Source<T>) {
    for (;;) {
        if (typeof p !== "function") {
            return p;
        }
        p = p();
    }
}

danielearwicker avatar Jan 08 '15 10:01 danielearwicker

Still awaiting on this :+1:

bluong avatar May 26 '15 22:05 bluong

+1

markvandenbrink avatar Jun 04 '15 09:06 markvandenbrink

:+1:

It would be great to be able to define type aliases like this:

type Predicate<T> = (element: T) => boolean;

mariusschulz avatar Jun 04 '15 11:06 mariusschulz

:+1:

robertpenner avatar Jun 04 '15 12:06 robertpenner

:+1:

blobor avatar Jun 04 '15 12:06 blobor

:+1: It would be great and helpful feature

flaticols avatar Jun 04 '15 13:06 flaticols

Since y'all asked: #3397. Enjoy!

ahejlsberg avatar Jun 05 '15 21:06 ahejlsberg

:clap: What a legend.

danielearwicker avatar Jun 05 '15 22:06 danielearwicker

Awesome! On Jun 5, 2015 5:59 PM, "Anders Hejlsberg" [email protected] wrote:

Since y'all asked: #3397 https://github.com/Microsoft/TypeScript/pull/3397. Enjoy!

— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/1616#issuecomment-109451967 .

zpdDG4gta8XKpMCd avatar Jun 05 '15 22:06 zpdDG4gta8XKpMCd

-)

Artazor avatar Jun 05 '15 22:06 Artazor

:+1:

tinganho avatar Jun 05 '15 23:06 tinganho

(Y)

danihodovic avatar Jun 10 '15 15:06 danihodovic

Awesome! My first try on this is type classOf<T> = {new(): T}

HerringtonDarkholme avatar Jun 11 '15 07:06 HerringtonDarkholme

@HerringtonDarkholme That seems like an odd use of generic type aliases. An interface seems like a better choice here:

interface classOf<T> {
    new(): T;
}

CyrusNajmabadi avatar Jun 11 '15 08:06 CyrusNajmabadi