镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Separate type application from function application #29043

Description

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

Activity

  1. canonic-epicure commented on Feb 14, 2019

    @canonic-epicure
    Author

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

  2. canonic-epicure commented on May 9, 2019

    @canonic-epicure
    Author

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

  3. bennypowers commented on May 25, 2020

    @bennypowers

    Checking in to see if this is on the radar

  4. joshuambg commented on Nov 6, 2022

    @joshuambg

    Please and thank you.

  5. maxpatiiuk commented on Feb 17, 2025

    @maxpatiiuk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions