tools / files / cheatsheets / apps / index.ts
index.ts
| 1 | const a = 1; |
| 2 | const b = 2; |
| 3 | const c = a + b; |
| 4 | console.log({ a, b, c }); |
| 5 | |
| 6 | const getUserInput1 = (): string | undefined => { |
| 7 | return "User Input"; |
| 8 | }; |
| 9 | const getUserInput2 = (): number | number[] => { |
| 10 | return [1, 2, 3]; |
| 11 | }; |
| 12 | |
| 13 | const input1 = getUserInput1(); |
| 14 | console.log(`1, type of input1: ${typeof input1}`); |
| 15 | if (typeof input1 === "string") { |
| 16 | console.log("2, input1 is a string:", input1); |
| 17 | } |
| 18 | |
| 19 | const input2 = getUserInput2(); |
| 20 | console.log(`3, type of input2: ${typeof input2}`); |
| 21 | if (input2 instanceof Array) { |
| 22 | console.log("4, input2 is an array:", input2); |
| 23 | } |
| 24 | |
| 25 | console.log(`5, done with type checks`); |
| 26 | |
| 27 | const typeString: string = "This is a string"; |
| 28 | const typeNumber: number = 42; |
| 29 | const typeBoolean: boolean = true; |
| 30 | const typeUndefined: undefined = undefined; |
| 31 | const typeNull: null = null; |
| 32 | const typeVoid: void = undefined; // void is often used for functions that do not return a value |
| 33 | const typeUnknown: unknown = "This could be anything"; |
| 34 | |
| 35 | console.log(`6, done with type checks`); |
| 36 | |
| 37 | const stringArray: string[] = ["apple", "banana", "cherry"]; |
| 38 | const numberArray: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 39 | if (Array.isArray(stringArray)) { |
| 40 | console.log("7, typeStringArray is an array:", stringArray); |
| 41 | } |
| 42 | if (Array.isArray(numberArray)) { |
| 43 | console.log("8, typeNumberArray is an array:", numberArray); |
| 44 | } |
| 45 | |
| 46 | let subNumArray: number[] = numberArray.slice(1, 6); // Get a sub-array from index 1 to 5 (not including index 6) |
| 47 | console.log(`8.1, subNumArray:`, subNumArray); |
| 48 | subNumArray.push(11); // Add 11 to the end of the array |
| 49 | subNumArray = subNumArray.filter(num => num % 2 === 0); // Filter even numbers |
| 50 | subNumArray.splice(2, 1, 12); // Replace the third element with 12 |
| 51 | subNumArray.shift(); // Remove the first element |
| 52 | subNumArray = subNumArray.concat([13, 14]); // Concatenate with another array |
| 53 | const arraySum = subNumArray.reduce((sum, curr) => sum + curr, 0); // Sum of elements |
| 54 | console.log(`8.2, arraySum:`, arraySum); |
| 55 | console.log(`8.3, subNumArray after changes:`, subNumArray); |
| 56 | |
| 57 | const namedFields: { id: number } = { id: 1 }; |
| 58 | if ("id" in namedFields) { |
| 59 | console.log("9, typeNamedFields has an id field:", namedFields.id); |
| 60 | } |
| 61 | |
| 62 | const object: { id: number; name: string } = { id: 1, name: "Object Name" }; |
| 63 | console.log(`10, typeObject:`, object); |
| 64 | console.log(`11, typeof typeObject:`, typeof object); |
| 65 | |
| 66 | const fn = (param: string): number => { return param.length; }; |
| 67 | console.log(`12, typeFunction:`, fn); |
| 68 | console.log(`13, typeof typeFunction:`, typeof fn); |
| 69 | console.log(`14, typeFunction result:`, fn("Hello")); |
| 70 | |
| 71 | const tuple: [number, string] = [1, "Tuple"]; |
| 72 | console.log(`15, typeTuple:`, tuple); |
| 73 | console.log(`16, typeof typeTuple:`, typeof tuple); |
| 74 | |
| 75 | const now = new Date(); |
| 76 | console.log(`17, Current Date:`, now); |
| 77 | console.log(`18, typeof now:`, typeof now); |
| 78 | |
| 79 | const yesterdayGD = new Date(now); |
| 80 | yesterdayGD.setDate(now.getDate() - 1); |
| 81 | console.log(`19, Yesterday's Date (using getDate()):`, yesterdayGD); |
| 82 | |
| 83 | const yesterdayGT = new Date(now.getTime() - 24 * 60 * 60 * 1000); |
| 84 | console.log(`20, Yesterday's Date (using getTime()):`, yesterdayGT); |
| 85 | |
| 86 | const durationMs = now.getTime() - yesterdayGD.getTime(); |
| 87 | console.log(`21, Duration in milliseconds:`, durationMs); |
| 88 | |
| 89 | const map: Map<string, number> = new Map([ |
| 90 | ["a", 1], |
| 91 | ["b", 2], |
| 92 | ["c", 3] |
| 93 | ]); |
| 94 | console.log(`22, map:`, map); |
| 95 | |
| 96 | map.set("d", 4); |
| 97 | map.set("b", 5); |
| 98 | map.delete("c"); |
| 99 | map.get("a"); |
| 100 | console.log(`23, map after modifications:`, map); |
| 101 | map.clear(); |
| 102 | |
| 103 | const set: Set<number> = new Set([1, 2, 3]); |
| 104 | console.log(`24, set:`, set); |
| 105 | set.add(4); |
| 106 | set.add(2); // Duplicate, will not be added |
| 107 | set.delete(1); |
| 108 | set.has(3); |
| 109 | set.size; |
| 110 | console.log(`25, set after modifications:`, set); |
| 111 | |
| 112 | const regex = /p=(\d+),(\d+) v=(-?\d+),(-?\d+)/; |
| 113 | const match = regex.exec("p=1,2 v=-3,4")!; |
| 114 | const pos = { |
| 115 | x: parseInt(match[1]!, 10), |
| 116 | y: parseInt(match[2]!, 10) |
| 117 | }; |
| 118 | const vel = { |
| 119 | x: parseInt(match[3]!, 10), |
| 120 | y: parseInt(match[4]!, 10) |
| 121 | }; |
| 122 | |
| 123 | interface Info { |
| 124 | id: number; |
| 125 | name: string; |
| 126 | coords: { |
| 127 | x: number; |
| 128 | y: number; |
| 129 | }; |
| 130 | expiration?: Date; |
| 131 | tags: string[] | null; |
| 132 | |
| 133 | getInfo: () => string; |
| 134 | findTag(tag: string): boolean; |
| 135 | |
| 136 | get size(): number; |
| 137 | set size(value: number | string); |
| 138 | } |
| 139 | const info: Info = { |
| 140 | id: 1, |
| 141 | name: "Info Object", |
| 142 | coords: { x: 10, y: 20 }, |
| 143 | expiration: new Date(), |
| 144 | tags: ["tag1", "tag2"], |
| 145 | |
| 146 | getInfo() { |
| 147 | return `ID: ${this.id}, Name: ${this.name}, Coords: (${this.coords.x}, ${this.coords.y})`; |
| 148 | }, |
| 149 | |
| 150 | findTag(tag: string): boolean { |
| 151 | return this.tags ? this.tags.includes(tag) : false; |
| 152 | }, |
| 153 | |
| 154 | get size(): number { |
| 155 | return this.tags ? this.tags.length : 0; |
| 156 | }, |
| 157 | set size(value: number | string) { |
| 158 | if (typeof value === "number") { |
| 159 | this.tags = Array(value).fill("defaultTag"); |
| 160 | } else if (typeof value === "string") { |
| 161 | this.tags = [value]; |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | info.size = 3; // Set size to 3, will fill with default tags |
| 166 | console.log(`26, info:`, info); |
| 167 | console.log(`27, info.getInfo():`, info.getInfo()); |
| 168 | console.log(`28, info.get size:`, info.size); |
| 169 | |
| 170 | interface DataSource<ResponseType> { |
| 171 | fetchData(): ResponseType; |
| 172 | } |
| 173 | |
| 174 | interface DataSource2<ResponseType extends { status: number }> { |
| 175 | fetchData(): ResponseType; |
| 176 | } |