TypeScript HelloWorld
·
// TypeScript - Typed superset of JavaScript
/*
TypeScript is a programming language developed and maintained by Microsoft.
It is a strict syntactical superset of JavaScript and adds optional static
typing to the language. TypeScript is designed for development of large
applications and transcompiles to JavaScript.
Key Features:
- Static typing
- Object-oriented programming
- Cross-platform compatibility
- ECMAScript compatibility
- Rich IDE support
- Gradual typing
- Interfaces and generics
*/
// Variables with type annotations
const language: string = "TypeScript";
let version: number = 5.0;
const isCompiled: boolean = true;
console.log(`Welcome to ${language}!`);
// Data types
const number: number = 42;
const decimal: number = 3.14159;
const isTyped: boolean = true;
const character: string = 'T';
// Control structures
if (isCompiled) {
console.log(`${language} is compiled to JavaScript`);
}
// Arrays
const features: string[] = ["Static Typing", "OOP", "Interfaces", "Generics"];
console.log("Key Features:");
features.forEach((feature, index) => {
console.log(`${index+1}. ${feature}`);
});
// Tuples
let tuple: [string, number] = ["Hello", 42];
console.log(`Tuple: ${tuple[0]}, ${tuple[1]}`);
// Enums
enum CompassPoint {
North,
South,
East,
West
}
let direction: CompassPoint = CompassPoint.North;
console.log(`Direction: ${CompassPoint[direction]}`);
// Objects with interfaces
interface Person {
name: string;
age: number;
email?: string; // Optional property
}
const person: Person = {
name: "Alice",
age: 30
};
console.log(`Person: ${person.name}, ${person.age}`);
// Functions with type annotations
function greet(name: string): string {
return `Hello, ${name}!`;
}
function add(a: number, b: number): number {
return a + b;
}
console.log(greet("Programmer"));
console.log(`5 + 3 = ${add(5, 3)}`);
// Classes
abstract class Animal {
constructor(protected name: string) {}
abstract speak(): void;
}
class Dog extends Animal {
constructor(name: string) {
super(name);
}
speak(): void {
console.log(`${this.name} says Woof!`);
}
}
const dog: Dog = new Dog("Buddy");
dog.speak();
// Generics
function identity<T>(arg: T): T {
return arg;
}
let output1 = identity<string>("Hello");
let output2 = identity<number>(42);
console.log(`Generic outputs: ${output1}, ${output2}`);
// Union types
type StringOrNumber = string | number;
function padLeft(value: string, padding: StringOrNumber) {
if (typeof padding === "number") {
return " ".repeat(padding) + value;
}
return padding + value;
}
console.log(padLeft("Hello", 5));
console.log(padLeft("Hello", "----"));
// Intersection types
interface Identifiable {
id: number;
}
interface Timestamped {
createdAt: Date;
}
type Model = Identifiable & Timestamped;
const model: Model = {
id: 1,
createdAt: new Date()
};
console.log(`Model ID: ${model.id}, Created: ${model.createdAt}`);
// Exception handling
try {
const result = 10 / 0;
} catch (error) {
console.log("Cannot divide by zero!");
}
console.log("TypeScript demo completed!");
更多推荐


所有评论(0)