镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Allow value pointing to a class or whose type has a new signature to be used as a type #45013

Closed

Description

Suggestion

🔍 Search Terms

refers to value used as type static class generic

✅ 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 a class is created by a static method, there doesn't seem to be a way to directly use it as a class type.

📃 Motivating Example

class BaseClass {
    constructor() {}

    static cloneClass() {
        return class extends this { };
    }
}

function takeBase(base: BaseClass) {} // Ok
takeBase(new BaseClass()); // Ok

const CreatedClass = BaseClass.cloneClass();

function takeCreated(created: CreatedClass) {}
//                            ~~~~~~~~~~~~
// 'CreatedClass' refers to a value, but is being used as a type here.
// Did you mean 'typeof CreatedClass'?

takeCreated(new CreatedClass()); // Ok

While CreatedClass is indeed a value, it's a value that happens to be equal to a class.

💻 Use Cases

In general, class factories like this are rather unwieldy to set up in the type system.

For a specific use case, see gr2m/javascript-plugin-architecture-with-typescript-definitions#57, as posted in SO at https://stackoverflow.com/questions/68201386/how-to-make-required-constructor-options-optional-when-they-were-set-using-mycla. Those Oktokit classes have a static defaults member that returns a new class. That new class has a constructor parameter whose type information must be informed by the previous static class' generic.

See this playground link for a version that uses an interface to describe the class, and this playground link for a verson that purely uses a static class. The former would be resolved nicely by this issue's requested feature; the latter would be doable in a more complex way if #26242 and/or #5863 are implemented.

cc Gregor Martynus (@gr2m)

Activity

  1. MartinJohns commented on Jul 13, 2021

    @MartinJohns
    Contributor

    But you can refer to it using the typeof operator. This works just fine:

    function takeCreated(created: InstanceType<typeof CreatedClass>) {}
    
  2. JoshuaKGoldberg commented on Jul 13, 2021

    @JoshuaKGoldberg
    ContributorAuthor

    Martin Johns (@MartinJohns) true, but that's got pretty bad ergonomics. See the linked issue -- library authors shouldn't have to force the constituents into using type shenanigans like that.

  3. MartinJohns commented on Jul 13, 2021

    @MartinJohns
    Contributor

    I really don't see how this is "type shenanigans". It's just how the type system is designed and works. And I'd say const variables automatically being imported to the types would be even worse ergonomics.

    But const variables not introducing new types is the intended behavior.

    Your feature request goes in the same direction as #44681.

  4. RyanCavanaugh commented on Jul 13, 2021

    @RyanCavanaugh
    Member

    Name resolution isn't allowed to have fallback behavior. Doing this creates a semantic trap where adding a new top-level type possibly changes the meaning of existing code. And obviously we can't change the behavior to prefer an inner-scoped const over an outer-scoped interface; this would be breaking beyond measure.

  5. typescript-bot commented on Jul 17, 2021

    @typescript-bot
    Contributor

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

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions