|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: TypeScript Getting Started |
| 4 | + description: A beginner-friendly, comprehensive guide to learning TypeScript from basics to intermediate concepts. |
| 5 | +hero: |
| 6 | + Hero: UndrawOnlineArticles |
| 7 | +tags: |
| 8 | + - memberresources |
| 9 | +order: 1 |
| 10 | +--- |
| 11 | + |
| 12 | + |
| 13 | +import TextContainer from '@/components/content/TextContainer'; |
| 14 | +import LeadText from '@/components/content/LeadText'; |
| 15 | + |
| 16 | + |
| 17 | +<TextContainer background="light" showBackToTopLink={false}> |
| 18 | +<LeadText> |
| 19 | + |
| 20 | +Welcome to the **TypeScript Getting Started** guide! <br />This resource is designed for developers of all levels who want to learn TypeScript from scratch and gradually build up to using types, interfaces, and generics in real-world code patterns. By the end of this guide, you'll be confident writing type-safe JavaScript applications. |
| 21 | + |
| 22 | +</LeadText> |
| 23 | +</TextContainer> |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +<TextContainer> |
| 28 | + |
| 29 | +## Table of Contents |
| 30 | + |
| 31 | +1. [Introduction](#introduction) |
| 32 | +2. [Installation and Setup](#installation-and-setup) |
| 33 | +3. [Basic Types](#basic-types) |
| 34 | +4. [Interfaces](#interfaces) |
| 35 | +5. [Generics](#generics) |
| 36 | +6. [Examples from Virtual Coffee Code Patterns](#examples-from-virtual-coffee-code-patterns) |
| 37 | +7. [Resources](#resources) |
| 38 | + |
| 39 | +</TextContainer> |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +<TextContainer background="light"> |
| 44 | + |
| 45 | +## Introduction |
| 46 | + |
| 47 | +TypeScript is a **superset of JavaScript** that adds **static types**. It helps catch errors early, improves code readability, and makes large codebases easier to maintain. |
| 48 | +Even if you're new to JavaScript, TypeScript is beginner-friendly and gradually introduces type annotations without forcing drastic changes. |
| 49 | + |
| 50 | +</TextContainer> |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +<TextContainer> |
| 55 | + |
| 56 | +## Installation and Setup |
| 57 | + |
| 58 | +You can install TypeScript globally or locally in a project. |
| 59 | + |
| 60 | +**Global installation:** |
| 61 | + |
| 62 | +```bash |
| 63 | +npm install -g typescript |
| 64 | +``` |
| 65 | + |
| 66 | +**Local installation(recommended):** |
| 67 | + |
| 68 | +```bash |
| 69 | +npm install --save-dev typescript |
| 70 | +``` |
| 71 | + |
| 72 | +<br /> |
| 73 | +**Initialize a TypeScript project with:** |
| 74 | + |
| 75 | +```bash |
| 76 | +tsc --init |
| 77 | +``` |
| 78 | +This creates a `tsconfig.json` file to configure TypeScript options. |
| 79 | + |
| 80 | +<br /> |
| 81 | +Optional: If you’re using Node.js directly, |
| 82 | +you can install ts-node to run TypeScript files without precompiling: |
| 83 | + |
| 84 | +```bash |
| 85 | +npm install -g ts-node |
| 86 | +ts-node yourfile_name.ts |
| 87 | +``` |
| 88 | +</TextContainer> |
| 89 | + |
| 90 | +<TextContainer background="light"> |
| 91 | +## Basic Types |
| 92 | + |
| 93 | +**TypeScript introduces static typing for variables and function parameters.** |
| 94 | + |
| 95 | +```ts |
| 96 | +let username: string = "Roman"; |
| 97 | +let age: number = 22; |
| 98 | +let isDeveloper: boolean = true; |
| 99 | +let anything: any = "can be anything"; // flexible but avoid overuse |
| 100 | +let unknownValue: unknown = "something unknown"; |
| 101 | +``` |
| 102 | +<br /> |
| 103 | +**Functions with typed parameters and return types:** |
| 104 | + |
| 105 | +```ts |
| 106 | +function greet(name: string): string { |
| 107 | + return `Hello, ${name}!`; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +<br /> |
| 112 | +**Arrays and Tuples:** |
| 113 | + |
| 114 | +```ts |
| 115 | +let numbers: number[] = [1, 2, 3]; |
| 116 | +let user: [string, number] = ["Roman", 22]; // tuple |
| 117 | +``` |
| 118 | + |
| 119 | +<br /> |
| 120 | +**Union Types:** |
| 121 | +```ts |
| 122 | +let id: string | number; |
| 123 | +id = "abc123"; |
| 124 | +id = 456; |
| 125 | +``` |
| 126 | + |
| 127 | +<br /> |
| 128 | +**Enums:** |
| 129 | + |
| 130 | +```ts |
| 131 | +enum Role { |
| 132 | + Admin, |
| 133 | + Member, |
| 134 | + Guest |
| 135 | +} |
| 136 | +let myRole: Role = Role.Member; |
| 137 | +``` |
| 138 | + |
| 139 | +</TextContainer> |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +<TextContainer> |
| 144 | +## Interfaces |
| 145 | + |
| 146 | +**Interfaces define the shape of objects and ensure consistent structures:** |
| 147 | + |
| 148 | +```ts |
| 149 | +interface User { |
| 150 | + name: string; |
| 151 | + age: number; |
| 152 | + isDeveloper?: boolean; // optional |
| 153 | +} |
| 154 | + |
| 155 | +const roman: User = { |
| 156 | + name: "Roman", |
| 157 | + age: 25, |
| 158 | +}; |
| 159 | +``` |
| 160 | + |
| 161 | +<br /> |
| 162 | +**Extending Interfaces:** |
| 163 | + |
| 164 | +```ts |
| 165 | +interface Employee extends User { |
| 166 | + department: string; |
| 167 | +} |
| 168 | + |
| 169 | +const alice: Employee = { |
| 170 | + name: "Alice", |
| 171 | + age: 30, |
| 172 | + department: "Engineering", |
| 173 | +}; |
| 174 | +``` |
| 175 | + |
| 176 | +<br /> |
| 177 | +**Interfaces are also useful for typing React props:** |
| 178 | + |
| 179 | +```ts |
| 180 | +interface ButtonProps { |
| 181 | + label: string; |
| 182 | + onClick: () => void; |
| 183 | +} |
| 184 | + |
| 185 | +function Button({ label, onClick }: ButtonProps) { |
| 186 | + return <button onClick={onClick}>{label}</button>; |
| 187 | +} |
| 188 | +``` |
| 189 | +</TextContainer> |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | +<TextContainer background="light"> |
| 194 | +## Generics |
| 195 | +Generics allow creating reusable components and functions while maintaining type safety: |
| 196 | + |
| 197 | +```ts |
| 198 | +function identity<T>(arg: T): T { |
| 199 | + return arg; |
| 200 | +} |
| 201 | + |
| 202 | +const result = identity<number>(42); |
| 203 | +const text = identity<string>("Hello TypeScript"); |
| 204 | +``` |
| 205 | + |
| 206 | +<br /> |
| 207 | +**Generic Interfaces:** |
| 208 | + |
| 209 | +```ts |
| 210 | +interface ApiResponse<T> { |
| 211 | + data: T; |
| 212 | + error?: string; |
| 213 | +} |
| 214 | + |
| 215 | +const response: ApiResponse<User> = { |
| 216 | + data: { name: "Roman", age: 25 }, |
| 217 | +}; |
| 218 | +``` |
| 219 | +`Generics are essential for building flexible, scalable code.` |
| 220 | +</TextContainer> |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +<TextContainer> |
| 225 | +## Examples from Virtual Coffee Code Patterns |
| 226 | +`Here’s a small example adapted to Virtual Coffee’s code style:` |
| 227 | + |
| 228 | +```ts |
| 229 | +interface Member { |
| 230 | + id: number; |
| 231 | + name: string; |
| 232 | + role: "Admin" | "Member"; |
| 233 | +} |
| 234 | + |
| 235 | +const members: Member[] = [ |
| 236 | + { id: 1, name: "Roman", role: "Member" }, |
| 237 | + { id: 2, name: "Ayu", role: "Admin" }, |
| 238 | +]; |
| 239 | + |
| 240 | +// Function to get all admin members |
| 241 | +function getAdmins<T extends Member>(list: T[]): T[] { |
| 242 | + return list.filter(member => member.role === "Admin"); |
| 243 | +} |
| 244 | + |
| 245 | +const admins = getAdmins(members); |
| 246 | +console.log(admins); |
| 247 | +``` |
| 248 | +This demonstrates interfaces, arrays, generics, and type-safe filtering. |
| 249 | +</TextContainer> |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | +<TextContainer background="light"> |
| 254 | +## Resources |
| 255 | +- [TypeScript Official Documentation](https://www.typescriptlang.org/docs/) |
| 256 | +- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/intro.html) |
| 257 | +- [Virtual Coffee Developer Resources](https://virtualcoffee.io/resources/developer-resources/) |
| 258 | +</TextContainer> |
| 259 | + |
| 260 | +<TextContainer style={{ textAlign: 'center', fontSize: '0.875rem', marginTop: '2rem' }}> |
| 261 | + Made with ❤️ [Technozamazing](https://github.com/Technozamazing) |
| 262 | +</TextContainer> |
0 commit comments