📅  最后修改于: 2023-12-03 15:01:07.847000             🧑  作者: Mango
TypeScript is a superset of JavaScript developed by Microsoft. It adds optional static typing, class-based object-oriented programming, and other features to JavaScript.
Using TypeScript can help catch errors before runtime, as well as provide better code organization and maintainability through object-oriented programming concepts.
To start using TypeScript, first install the TypeScript compiler using npm install -g typescript
. Then, create a .ts
file and run tsc filename.ts
to compile it into JavaScript.
// Static typing
let variableName: string = "Hello World";
// Classes and Inheritance
class Animal {
speak() {
console.log("Animal makes a noise");
}
}
class Cat extends Animal {
speak() {
console.log("Cat meows");
}
}
let myAnimal: Animal = new Cat();
myAnimal.speak(); // Prints "Cat meows"
// Interfaces
interface Person {
name: string;
age?: number;
}
function greet(person: Person) {
console.log(`Hello, ${person.name}`);
}
greet({ name: "John" }); // Prints "Hello, John"
Happy coding! 💻👨💻👩💻