Skip to content

Utility type - "Instantiable" #51841

Not planned
Not planned
@jlachniet

Description

@jlachniet

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.

Activity

added
SuggestionAn idea for TypeScript
DeclinedThe issue was declined as something which matches the TypeScript vision
on Dec 10, 2022
RyanCavanaugh

RyanCavanaugh commented on Dec 10, 2022

@RyanCavanaugh
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

typescript-bot commented on Jun 21, 2023

@typescript-bot
Collaborator

This issue has been marked as "Declined" 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RyanCavanaugh@typescript-bot@jlachniet

        Issue actions

          Utility type - "Instantiable" Β· Issue #51841 Β· microsoft/TypeScript