Skip to content
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

Utility type - "Instantiable" #51841

Closed
5 tasks done
jlachniet opened this issue Dec 10, 2022 · 2 comments
Closed
5 tasks done

Utility type - "Instantiable" #51841

jlachniet opened this issue Dec 10, 2022 · 2 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@jlachniet
Copy link

Suggestion

πŸ” Search Terms

instantiable, utility type

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

When writing a TypeScript decorator, it's useful to have a type representing any function or class that can be instantiated. This can be described using a custom type like:

type Instantiable = new (...args: never) => unknown;

This allows you describe an instantiable function or class without resorting to any or unknown. I think that including this utility type as part of the built in TypeScript types would be helpful.

πŸ“ƒ Motivating Example

I created a library a while ago can serialize instances of classes. I wrote a Serializable decorator that registers classes for serialization. The decorator needs to access target.prototype which doesn't work if target is unknown.

type Instantiable = new (...args: never) => unknown;

function Serializable(identifier: string) {
	return (target: Instantiable) => register(target.prototype, identifier);
}

πŸ’» Use Cases

The easiest solution is just to declare your own Instantiable type. You can also use unknown or any, which are less than ideal.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Dec 10, 2022
@RyanCavanaugh
Copy link
Member

We don't add utility types to the library unless they're required for declaration emit

See also https://twitter.com/SeaRyanC/status/1540022633344905216, #39522 (comment)

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Declined" 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 Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants