microsoft/TypeScript

Separate type application from function application

canonic-epicure opened this issue · 5 comments

As discussed in #28931, currently the type application for the function with generic parameters is tied to that function application.

Specialize the value of the function with generics is only possible during the call. So the following compiles fine:

function id<V> (v : V) { return v }

const some : Date = id<Date>(new Date())

But this does not:

function id<V> (v : V) { return v }

type IdDate         = typeof id<Date> // TS1005: ';' expected 

const dateId        = id<Date> // TS1109: Expression expected.

It would be very beneficial to separate type application from function application. For example (the original reason of this request), it will allow mixins with generic parameters:

export type Constructable<T extends any> = new (...args : any[]) => T
export type AnyFunction             = (...input: any[]) => any
export type Mixin<T extends AnyFunction> = InstanceType<ReturnType<T>>

export const Atom = <V, T extends Constructable<Object>>(base : T) =>

class Atom extends base {
    value               : V

    hasValue () : boolean {
        return this.hasOwnProperty('value')
    }
}

export type Atom = Mixin<typeof Atom> // this currently works, but does not have generic argument

export type Atom<V> = Mixin<typeof Atom<V>> // this is the goal

Any hope for this feature? I think it will unblock many other advanced use cases.

Any hope for a proper solution? Having a generic argument for a mixin function is a very common requirement.

Checking in to see if this is on the radar

Please and thank you.