้•œๅƒ็ซ™็‚น ยท ๆœฌ้กต็”ฑ็ฌฌไธ‰ๆ–น GitHub ๅช่ฏป้•œๅƒๆไพ›๏ผŒ้ž GitHub ๅฎ˜ๆ–น็ซ™็‚น๏ผŒไธๆŽฅๅ—ไปปไฝ•็™ปๅฝ•ๆˆ–ๅ‡ญๆฎ่พ“ๅ…ฅใ€‚ๅ‰ๅพ€ github.com
Skip to content

Utility type - "Instantiable"ย #51841

Closed as not planned
Closed as not planned

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

  1. 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)

  2. typescript-bot commented on Jun 21, 2023

    @typescript-bot
    Contributor

    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

      Issue actions