microsoft / microsoft/TypeScript

Separate type application from function application

Open
#29,043 5 comments 42 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the discussion in issue #28931 first, then use the examples here to identify the intended syntax and its interaction with generic functions, type queries, and mixins. Done means the proposed type application can be used independently of a function call and the shown examples compile as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.