1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-10-17 01:03:52 +01:00

chore: update highlighted test Markdown with embedded Typescript file

This commit is contained in:
Keith Hall
2025-10-14 22:02:16 +03:00
parent 0139c9d9ae
commit 8920c738c5

View File

@@ -1,57 +1,57 @@
# Typescript test # Typescript test
```typescript ```typescript
enum Status { enum Status {
 Pending,  Pending,
 InProgress,  InProgress,
 Completed,  Completed,
} }
interface Task { interface Task {
 id: number;  id: number;
 title: string;  title: string;
 status: Status;  status: Status;
 assignee?: string;  assignee?: string;
} }
class TaskManager<T extends Task> { class TaskManager<T extends Task> {
 private tasks: T[] = [];  private tasks: T[] = [];
 addTask(task: T): void {  addTask(task: T): void {
 this.tasks.push(task);  this.tasks.push(task);
 }  }
 getTasksByStatus(status: Status): T[] {  getTasksByStatus(status: Status): T[] {
 return this.tasks.filter(task => task.status === status);  return this.tasks.filter(task => task.status === status);
 }  }
 async fetchTasks(): Promise<T[]> {  async fetchTasks(): Promise<T[]> {
 // Simulate async fetch  // Simulate async fetch
 return new Promise(resolve => setTimeout(() => resolve(this.tasks), 500));  return new Promise(resolve => setTimeout(() => resolve(this.tasks), 500));
 }  }
} }
// Type guard // Type guard
function isTask(obj: any): obj is Task { function isTask(obj: any): obj is Task {
 return typeof obj.id === 'number' && typeof obj.title === 'string';  return typeof obj.id === 'number' && typeof obj.title === 'string';
} }
// Usage // Usage
const manager = new TaskManager<Task>(); const manager = new TaskManager<Task>();
manager.addTask({ id: 1, title: "Write docs", status: Status.Pending }); manager.addTask({ id: 1, title: "Write docs", status: Status.Pending });
manager.addTask({ id: 2, title: "Review PR", status: Status.InProgress, assignee: "Alice" }); manager.addTask({ id: 2, title: "Review PR", status: Status.InProgress, assignee: "Alice" });
(async () => { (async () => {
 const allTasks = await manager.fetchTasks();  const allTasks = await manager.fetchTasks();
 allTasks.forEach(task => {  allTasks.forEach(task => {
 if (isTask(task)) {  if (isTask(task)) {
 console.log(`Task #${task.id}: ${task.title} [${Status[task.status]}]`);  console.log(`Task #${task.id}: ${task.title} [${Status[task.status]}]`);
 }  }
 });  });
})(); })();
// Type assertion // Type assertion
const unknownValue: unknown = { id: 3, title: "Test", status: Status.Completed }; const unknownValue: unknown = { id: 3, title: "Test", status: Status.Completed };
const assertedTask = unknownValue as Task; const assertedTask = unknownValue as Task;
console.log(assertedTask.title); console.log(assertedTask.title);
``` ```