Skip to content

Allow class expressions in type declarationsΒ #45014

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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.

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