typescript / beginner
Snippet
Simple Class Declaration
Classes in TypeScript use type annotations for their properties and methods to provide better structure and editor support.
snippet.ts
1
2
3
4
5
6
7
8
9
10
11
class Robot {model: string;constructor(modelName: string) {this.model = modelName;}identify(): string {return "I am model " + this.model;}}
Breakdown
1
model: string;
Declares a property 'model' which is of type string.
2
constructor(modelName: string)
Ensures that the class is initialized with a string value.
3
identify(): string
Defines a method that returns a string result.