Skip to content

typeof class #41581

Open
Open
@DanielRosenwasser

Description

@DanielRosenwasser

@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;

Activity

added
SuggestionAn idea for TypeScript
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
on Nov 18, 2020
ExE-Boss

ExE-Boss commented on Nov 19, 2020

@ExE-Boss
Contributor

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

jakebailey

jakebailey commented on Nov 21, 2023

@jakebailey
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

Metadata

Metadata

Labels

Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @DanielRosenwasser@ExE-Boss@ahejlsberg@jakebailey

    Issue actions

      `typeof class` · Issue #41581 · microsoft/TypeScript