Skip to content

Introduce Class or Constructor utility type #56286

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
DanKing123 opened this issue Nov 1, 2023 · 2 comments
Closed
5 tasks done

Introduce Class or Constructor utility type #56286

DanKing123 opened this issue Nov 1, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@DanKing123
Copy link

DanKing123 commented Nov 1, 2023

πŸ” Search Terms

Class Constructor utility type

βœ… Viability Checklist

  • 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

Angular includes a Type utility type which is defined like this:

export declare interface Type<T> extends Function {
    new (...args: any[]): T;
}

I think this could be more succinctly defined, and I think the word "Type" here is a little misleading, as this actually denotes a class/constructor.

I'm wondering though, why TypeScript doesn't have a built-in equivalent? (Or if it does, why I can't find it, and it isn't documented here: https://www.typescriptlang.org/docs/handbook/utility-types.html)

I was thinking something like this:

export type Constructor<T> = new(...args: any[]) => T;

or maybe even:

export type Class<T> = new(...args: any[]) => T;

It's easy enough to hand-roll, but just feels like something that ought to be built into the language?

πŸ“ƒ Motivating Example

This pattern is already used extensively in many on-line code examples. E.g. see here: https://dev.to/kalashin1/utility-types-in-typescript-4nfo - although this post suggests TypeScript already has this feature, whereas as far as I can tell, it actually doesn't!

πŸ’» Use Cases

I've used the Angular Type utility type quite extensively in my Angular application, whenever I want to pass around classes for some reason, whilst also keeping track of their associated instance types.

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 1, 2023

The team decided against adding more utility types, unless they're needed for emit purposes. There's even a PR that will add "not a request for a utility type" to the viability checklist: #55746

And of course there's already #26990, #54428, #20256 and many more. Used search terms: constructor type in:title

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 1, 2023
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 2023
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

4 participants