Skip to content

Type-Checking Static Members of a Class #2636

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

Closed

Conversation

ajvincent
Copy link

This is something that was non-obvious to me at first glance. I figured writing down a simple example for others would not hurt.

@ajvincent
Copy link
Author

@microsoft-github-policy-service agree

@ajvincent
Copy link
Author

I'm thinking about moving this down to the next section, "Static blocks in classes", as an illustrative example which fits better there.

interface VehicleType {
  manufacturer: string
}

interface VehicleTypeStatic {
  readonly wheelCount: number;
}

class Vehicle implements VehicleType 
{
  static {
    const temp: VehicleTypeStatic = Vehicle;
    void(temp);
  }

  readonly manufacturer: string;

  constructor(maker: string) {
    this.manufacturer = maker;
  }

  static readonly wheelCount = 4;
}

I'm wondering what people think of this alternative.

@@ -909,6 +909,38 @@ const MyHelperObject = {
};
```

### Type-Checking Static Members of a Class

Under the hood, each class is an object (really a function) with a prototype property. You can define a type for the class's static members, which will be properties of the class (not instances of it).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure that this section is worth including, at least not as-is.

Are you hoping to have an example that shows how to work around the lack of class Vehicle extends static VehicleTypeStatic? There's probably a simpler way to do that, if so; I'd think we'd want to be pretty targeted about showing the difference between the the "value" of the class and the "type" of a class instance. Not sure if this doc already includes some of that language, though.

@ajvincent
Copy link
Author

I may not be able to respond in a timely manner. I have a new job which takes up a lot more of my time than I've been used to. If anyone wants to close this pull request, I have no objection.

@jakebailey jakebailey closed this May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants