1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-10-16 08:43:50 +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
enum Status {
 Pending,
 InProgress,
 Completed,
enum Status {
 Pending,
 InProgress,
 Completed,
}
interface Task {
 id: number;
 title: string;
 status: Status;
 assignee?: string;
interface Task {
 id: number;
 title: string;
 status: Status;
 assignee?: string;
}
class TaskManager<T extends Task> {
 private tasks: T[] = [];
class TaskManager<T extends Task> {
 private tasks: T[] = [];
 addTask(task: T): void {
 this.tasks.push(task);
 }
 addTask(task: T): void {
 this.tasks.push(task);
 }
 getTasksByStatus(status: Status): T[] {
 return this.tasks.filter(task => task.status === status);
 }
 getTasksByStatus(status: Status): T[] {
 return this.tasks.filter(task => task.status === status);
 }
 async fetchTasks(): Promise<T[]> {
 // Simulate async fetch
 return new Promise(resolve => setTimeout(() => resolve(this.tasks), 500));
 }
 async fetchTasks(): Promise<T[]> {
 // Simulate async fetch
 return new Promise(resolve => setTimeout(() => resolve(this.tasks), 500));
 }
}
// Type guard
function isTask(obj: any): obj is Task {
 return typeof obj.id === 'number' && typeof obj.title === 'string';
// Type guard
function isTask(obj: any): obj is Task {
 return typeof obj.id === 'number' && typeof obj.title === 'string';
}
// Usage
const manager = new TaskManager<Task>();
manager.addTask({ id: 1, title: "Write docs", status: Status.Pending });
manager.addTask({ id: 2, title: "Review PR", status: Status.InProgress, assignee: "Alice" });
// Usage
const manager = new TaskManager<Task>();
manager.addTask({ id: 1, title: "Write docs", status: Status.Pending });
manager.addTask({ id: 2, title: "Review PR", status: Status.InProgress, assignee: "Alice" });
(async () => {
 const allTasks = await manager.fetchTasks();
 allTasks.forEach(task => {
 if (isTask(task)) {
 console.log(`Task #${task.id}: ${task.title} [${Status[task.status]}]`);
 }
 });
})();
(async () => {
 const allTasks = await manager.fetchTasks();
 allTasks.forEach(task => {
 if (isTask(task)) {
 console.log(`Task #${task.id}: ${task.title} [${Status[task.status]}]`);
 }
 });
})();
// Type assertion
const unknownValue: unknown = { id: 3, title: "Test", status: Status.Completed };
const assertedTask = unknownValue as Task;
console.log(assertedTask.title);
// Type assertion
const unknownValue: unknown = { id: 3, title: "Test", status: Status.Completed };
const assertedTask = unknownValue as Task;
console.log(assertedTask.title);
```