Description
Suggestion
π Search Terms
instantiable, utility type
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis 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.
Activity
RyanCavanaugh commentedon Dec 10, 2022
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)
Constructor
type #54428type Awaitable<T> = T | PromiseLike<T>
#31394typescript-bot commentedon Jun 21, 2023
This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes.