📜  typescript 代码区域 - TypeScript (1)

📅  最后修改于: 2023-12-03 15:35:24.346000             🧑  作者: Mango

TypeScript代码区域 - TypeScript

TypeScript is a superset of JavaScript that adds optional static typing, classes, and interfaces to the language. It was developed by Microsoft and first released in 2012. TypeScript code is transpiled into JavaScript so that it can run in any environment that supports JavaScript.

Here is an example of a TypeScript class that defines a person:

class Person {
  private name: string;
  private age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  public sayHello(): void {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

const person = new Person("John", 30);
person.sayHello();

This code defines a Person class with private properties name and age, and a sayHello method that logs a greeting to the console. An instance of the Person class is then created and the sayHello method is called on it.

TypeScript provides several benefits over pure JavaScript, including:

  • Type checking and static analysis during development, which can catch errors before runtime.
  • Better code organization and maintainability through the use of classes and interfaces.
  • Improved tooling and IDE support, as TypeScript provides richer information about the code than plain JavaScript.

Overall, TypeScript is a great choice for large, complex projects that require maintainability and scalability.