Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow class expressions in type declarations #45014

Closed
5 tasks done
JoshuaKGoldberg opened this issue Jul 13, 2021 · 3 comments
Closed
5 tasks done

Allow class expressions in type declarations #45014

JoshuaKGoldberg opened this issue Jul 13, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@JoshuaKGoldberg
Copy link
Contributor

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.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Cannot represent class expression in type declaration: "Cannot find name 'class'" Allow class expression in type declarations Jul 13, 2021
@JoshuaKGoldberg JoshuaKGoldberg changed the title Allow class expression in type declarations Allow class expressions in type declarations Jul 13, 2021
@fatcerberus
Copy link

typeof BaseClass?

@RyanCavanaugh
Copy link
Member

Same as #41581 / #41587

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 13, 2021
@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Jul 14, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants