function identity<T>(arg: T): T {
return arg;
}
const result = identity<string>("Hello, Generics!");
console.log(result); // Output: Hello, Generics!
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
// app.ts
import { add } from "./math";
const sum = add(5, 3);
console.log(sum); // Output: 8
// moduleA.ts
export const message = "Hello from Module A!";
// moduleB.ts
import { message } from "./moduleA";
console.log(message); // Output: Hello from Module A!
// moduleA.ts
export function greet() {
console.log("Hello!");
}
// moduleB.ts
export function farewell() {
console.log("Goodbye!");
}
// main.ts
export * from "./moduleA";
export * from "./moduleB";
// app.ts
import * as utils from "./main";
utils.greet(); // Output: Hello!
utils.farewell(); // Output: Goodbye!
// myNamespace.ts
namespace MyNamespace {
export function greet() {
console.log("Hello from MyNamespace!");
}
}
// app.ts
MyNamespace.greet(); // Output: Hello from MyNamespace!
// shapes.ts
namespace Shapes {
export interface Shape {
area(): number;
}
export class Circle implements Shape {
constructor(private radius: number) {}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
}
// app.ts
const circle = new Shapes.Circle(5);
console.log(circle.area()); // Output: 78.53981633974483