The same as in other "analyzers" Hegel doesn't provide a special syntax for class (only type syntax for properties and methods). You can define class the same as in JavaScript, but Hegel will create a type for this class and it's constructor.
Playground
class User {name: string;password: string;constructor(name: string, password: string): User {this.name = name;this.password = password;}}const user: User = new User("any.user@gmail.com", "qwerty");// Type is "class User"const UserConstructor = User;
Class Methods
As with Function Types, Hegel provides syntax for argument types and return type;
Playground
class Fighter {tellTheRules(isNewbee: boolean): string {return isNewbee? "I don't know the rules.": `1. You do not talk about Fight Club.2. You DO NOT talk about Fight Club.3. If someone yells “stop!”, goes limp, or taps out, the fight is over.4. Only two guys to a fight.5. One fight at a time, fellas.6. The fights are bare knuckle. No shirt, no shoes, no weapons.7. Fights will go on as long as they have to.8. If this is your first time at Fight Club, you have to fight.`;}}
Class Fields
If you want to use a class field you must define it first and give it an annotation.
Playground
class Fighter {tellTheRules(): string {// Error: Property "isNewbie" does not exist in "Fighter"return this.isNewbie? "I don't know the rules.": `1. You do not talk about Fight Club.2. You DO NOT talk about Fight Club.3. If someone yells “stop!”, goes limp, or taps out, the fight is over.4. Only two guys to a fight.5. One fight at a time, fellas.6. The fights are bare knuckle. No shirt, no shoes, no weapons.7. Fights will go on as long as they have to.8. If this is your first time at Fight Club, you have to fight.`;}}
You can define class fields inside the body of class.
Playground
class Fighter {isNewbie: boolean = true;tellTheRules(): string {return this.isNewbie? "I don't know the rules.": `1. You do not talk about Fight Club.2. You DO NOT talk about Fight Club.3. If someone yells “stop!”, goes limp, or taps out, the fight is over.4. Only two guys to a fight.5. One fight at a time, fellas.6. The fights are bare knuckle. No shirt, no shoes, no weapons.7. Fights will go on as long as they have to.8. If this is your first time at Fight Club, you have to fight.`;}}
If you are already familiar with TypeScript, Hegel does not provide accessibility modifiers like
public
,private
orprotected
.
Static Members
The same as in JavaScript, static members of class will be inside the class constructor and not inside the class instance.
Playground
class FightClub {static tellTheRules(): string {return `1. You do not talk about Fight Club.2. You DO NOT talk about Fight Club.3. If someone yells “stop!”, goes limp, or taps out, the fight is over.4. Only two guys to a fight.5. One fight at a time, fellas.6. The fights are bare knuckle. No shirt, no shoes, no weapons.7. Fights will go on as long as they have to.8. If this is your first time at Fight Club, you have to fight.`;}}const fightClubRules = FightClub.tellTheRules(); // 👌!const fightClubInNYC = new FightClub();// Error: Property "tellTheRules" does not exist in "FightClub"const localFightClubRules = fightClubInNYC.tellTheRules();