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

typeof class #41581

Open
DanielRosenwasser opened this issue Nov 18, 2020 · 2 comments · May be fixed by #41587
Open

typeof class #41581

DanielRosenwasser opened this issue Nov 18, 2020 · 2 comments · May be fixed by #41587
Assignees
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

@ahejlsberg has had a few experiments to try to make it easier to describe the type of a class. Specifically:

  • avoiding the awkward static/instance pattern that users need to write on anonymous object types/interface types

    var Thing: {
      new () => {
        instanceField: number;
      }
      staticField: string;
    }
  • allowing users to describe types with class-specific characteristics (e.g. abstract)

The idea is to allow typeof to be followed by a class expression.

var Thing: typeof class Thing {
  static staticField: string;

  instanceField: number;
  constructor() {}
}

There are definitely some subtleties here. For example, handling loops and whether this syntax is allowed in JSDoc with a TypeScript-annotated class:

/** @type { typeof class C<T> { x: T } } */
//                        ^^^    ^^^
// TypeScript type parameters and annotations
// Are these allowed?
var Thing;
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Nov 18, 2020
@ahejlsberg ahejlsberg linked a pull request Nov 18, 2020 that will close this issue
@ExE-Boss
Copy link
Contributor

Would this make it possible to revisit microsoft/TypeScript-DOM-lib-generator#858 and #39504 (comment)?

@jakebailey
Copy link
Member

This is coming up again for isolated declarations; what is preventing us from emitting for class expressions in d.ts initializers, then referencing them that way?

export const a = class A {
    readonly a: A = new A();
}

// emits as

export const a = class A {
    readonly a: A;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants