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

Allow class expressions in type declarations #45014

Description

Suggestion

🔍 Search Terms

class expression declaration 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 ofTypeScript'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

Implementation:

class BaseClass {
    static cloneClass() {
        return class extends BaseClass { }
    }
}

How I would expect the declaration to look, with the type error present now:

declare class BaseClass {
    static cloneClass(): class extends BaseClass {};
    //                   ~~~~~
    // Cannot find name 'class'.
}

The declaration actually created:

declare class BaseClass {
    static cloneClass(): {
        new (): {};
        cloneClass(): any;
    };
}

💻 Use Cases

Similar to #45013: it's difficult to represent class "factories" like this one in type declarations.

Activity

  1. changed the title [-]Cannot represent class expression in type declaration: "Cannot find name 'class'"[/-] [+]Allow class expression in type declarations[/+] on Jul 13, 2021
  2. changed the title [-]Allow class expression in type declarations[/-] [+]Allow class expressions in type declarations[/+] on Jul 13, 2021
  3. fatcerberus commented on Jul 13, 2021

    @fatcerberus

    typeof BaseClass?

  4. RyanCavanaugh commented on Jul 13, 2021

    @RyanCavanaugh
    Member

    Same as #41581 / #41587

  5. JoshuaKGoldberg commented on Jul 14, 2021

    @JoshuaKGoldberg
    ContributorAuthor

    Super, thank you! Apologies for the issue spam then :)

  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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions