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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/documentation/copy/en/handbook-v2/Classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


```ts twoslash
interface VehicleType {
manufacturer: string
}

interface VehicleTypeStatic {
readonly wheelCount: number;
}

class Vehicle implements VehicleType {
readonly manufacturer: string;

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

static readonly wheelCount = 4;
}

const Mechanic = {
classHasFourWheels(value: VehicleTypeStatic) : boolean {
return value.wheelCount === 4;
}
};

Mechanic.classHasFourWheels(Vehicle); // returns true
```

## `static` Blocks in Classes

Static blocks allow you to write a sequence of statements with their own scope that can access private fields within the containing class. This means that we can write initialization code with all the capabilities of writing statements, no leakage of variables, and full access to our class's internals.
Expand Down