diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 20:22:09 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 20:22:09 +0100 |
| commit | 5a8dbc6347b3541e84fe669b22c17ad3b715e258 (patch) | |
| tree | b148c450939688caaaeb4adac6f2faa1eaffe649 /samples/test.ts | |
| download | qwe-editor-5a8dbc6347b3541e84fe669b22c17ad3b715e258.tar.gz | |
Engage!
Diffstat (limited to 'samples/test.ts')
| -rw-r--r-- | samples/test.ts | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/samples/test.ts b/samples/test.ts new file mode 100644 index 0000000..ebbea8e --- /dev/null +++ b/samples/test.ts | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | // sample.ts | ||
| 2 | // A practical TypeScript tour in one file 🚀 | ||
| 3 | |||
| 4 | // -------------------- | ||
| 5 | // Basic Types | ||
| 6 | // -------------------- | ||
| 7 | let username: string = "Alice"; | ||
| 8 | let age: number = 30; | ||
| 9 | let isAdmin: boolean = false; | ||
| 10 | |||
| 11 | let hobbies: string[] = ["gaming", "coding"]; | ||
| 12 | let scores: Array<number> = [10, 20, 30]; | ||
| 13 | |||
| 14 | // Tuple | ||
| 15 | let userTuple: [string, number] = ["Bob", 42]; | ||
| 16 | |||
| 17 | // -------------------- | ||
| 18 | // Enums | ||
| 19 | // -------------------- | ||
| 20 | enum Role { | ||
| 21 | User = "USER", | ||
| 22 | Admin = "ADMIN", | ||
| 23 | Moderator = "MODERATOR", | ||
| 24 | } | ||
| 25 | |||
| 26 | // -------------------- | ||
| 27 | // Interfaces & Types | ||
| 28 | // -------------------- | ||
| 29 | interface User { | ||
| 30 | id: number; | ||
| 31 | name: string; | ||
| 32 | role: Role; | ||
| 33 | email?: string; // optional | ||
| 34 | } | ||
| 35 | |||
| 36 | type ApiResponse<T> = { | ||
| 37 | success: boolean; | ||
| 38 | data: T; | ||
| 39 | error?: string; | ||
| 40 | }; | ||
| 41 | |||
| 42 | // -------------------- | ||
| 43 | // Functions | ||
| 44 | // -------------------- | ||
| 45 | function greet(user: User): string { | ||
| 46 | return `Hello, ${user.name}! Your role is ${user.role}.`; | ||
| 47 | } | ||
| 48 | |||
| 49 | const add = (a: number, b: number): number => a + b; | ||
| 50 | |||
| 51 | // Function with default value | ||
| 52 | function log(message: string, level: "info" | "warn" | "error" = "info") { | ||
| 53 | console.log(`[${level.toUpperCase()}] ${message}`); | ||
| 54 | } | ||
| 55 | |||
| 56 | // -------------------- | ||
| 57 | // Classes | ||
| 58 | // -------------------- | ||
| 59 | class UserService { | ||
| 60 | private users: User[] = []; | ||
| 61 | |||
| 62 | constructor(initialUsers: User[] = []) { | ||
| 63 | this.users = initialUsers; | ||
| 64 | } | ||
| 65 | |||
| 66 | addUser(user: User): void { | ||
| 67 | this.users.push(user); | ||
| 68 | } | ||
| 69 | |||
| 70 | findByRole(role: Role): User[] { | ||
| 71 | return this.users.filter(u => u.role === role); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | // -------------------- | ||
| 76 | // Generics | ||
| 77 | // -------------------- | ||
| 78 | function wrapResponse<T>(data: T): ApiResponse<T> { | ||
| 79 | return { | ||
| 80 | success: true, | ||
| 81 | data, | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | // -------------------- | ||
| 86 | // Async / Await | ||
| 87 | // -------------------- | ||
| 88 | async function fetchUser(id: number): Promise<User> { | ||
| 89 | // Fake async call | ||
| 90 | return new Promise(resolve => { | ||
| 91 | setTimeout(() => { | ||
| 92 | resolve({ | ||
| 93 | id, | ||
| 94 | name: "Charlie", | ||
| 95 | role: Role.User, | ||
| 96 | }); | ||
| 97 | }, 500); | ||
| 98 | }); | ||
| 99 | } | ||
| 100 | |||
| 101 | // -------------------- | ||
| 102 | // Type Narrowing | ||
| 103 | // -------------------- | ||
| 104 | function printValue(value: string | number) { | ||
| 105 | if (typeof value === "string") { | ||
| 106 | console.log("String value:", value.toUpperCase()); | ||
| 107 | } else { | ||
| 108 | console.log("Number value:", value.toFixed(2)); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | // -------------------- | ||
| 113 | // Usage Example | ||
| 114 | // -------------------- | ||
| 115 | (async () => { | ||
| 116 | const user: User = { | ||
| 117 | id: 1, | ||
| 118 | name: username, | ||
| 119 | role: isAdmin ? Role.Admin : Role.User, | ||
| 120 | }; | ||
| 121 | |||
| 122 | log(greet(user)); | ||
| 123 | |||
| 124 | const service = new UserService([user]); | ||
| 125 | service.addUser({ id: 2, name: "Dana", role: Role.Admin }); | ||
| 126 | |||
| 127 | console.log("Admins:", service.findByRole(Role.Admin)); | ||
| 128 | |||
| 129 | const fetchedUser = await fetchUser(3); | ||
| 130 | const response = wrapResponse(fetchedUser); | ||
| 131 | |||
| 132 | console.log("API response:", response); | ||
| 133 | |||
| 134 | printValue("hello"); | ||
| 135 | printValue(123.456); | ||
| 136 | |||
| 137 | console.log("2 + 3 =", add(2, 3)); | ||
| 138 | })(); | ||
