Skip to content

Reference class type parameters in static members #32246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
ardalanamini opened this issue Jul 4, 2019 · 2 comments
Closed
5 tasks done

Reference class type parameters in static members #32246

ardalanamini opened this issue Jul 4, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ardalanamini
Copy link

Search Terms

static, generic, type, class, model, inheritance

Suggestion

Static class members should be allowed to reference the class's type parameters.

Use Cases

A very simple use case is in database models such as the example given below.
At the moment there's no way provided by the TypeScript to deal with such a problem which is the main reason we use this language, type safety!

Examples

Expected behavior:

class Model<T extends object = any> {
  public static all(): Promise<T[]> {
    // code
  }

  public static first(): Promise<T> {
    // code
  }
}

class User extends Model<{ username: string }> {}

User.all(); // Promise<Array<{ username: string }>>
User.first(); // Promise<{ username: string }>

The behavior we got to deal with: (which can add more typing error easily)

class Model {
  public static all<T extends object = any>(): Promise<T[]> {
    // code
  }

  public static first<T extends object = any>(): Promise<T> {
    // code
  }
}

class User extends Model {}

User.all<{ username: string }>(); // Promise<Array<{ username: string }>>
User.first<{ username: string }>(); // Promise<{ username: string }>

This bahavior is unacceptable since it needs the developer to reference the item interface every time he/she calls these sort of methods, which doesn't seem to satisfy the soul purpose of the TypeScript

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Duplicate An existing issue was already created and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Jul 8, 2019
@RyanCavanaugh
Copy link
Member

Duplicate
#32211
#24018
and probably others

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants