JavaScript แ แ แแซแซ แแ! Error แณแญแแ แแญ แฅแ แแตแ แซแณแซแแ! ๐ท
TypeScript catches bugs BEFORE you run your code โ like having a super-smart assistant reviewing every line!
TypeScript = JavaScript + Type System! Microsoft แฐแซแแข แแญ แฅแ JavaScript แ แแ แซแฐแซแแ โ แจ Run แแตแ Error แซแณแซแแ!
TypeScript is a superset of JavaScript โ all JS is valid TS, but TS adds type safety that catches bugs at compile time, before running!
TypeScript แแตแฅ แแแ variable type แ แแ โ string, number, boolean, array... TypeScript แแตแ แซแจแแแฃแ!
Every variable in TypeScript has a type. TypeScript verifies correct usage at compile time โ before you ever run the code!
// โโ Basic Types โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ let name: string = "Abel Tesfaye"; let age: number = 25; let active: boolean = true; let skills: string[] = ["HTML", "CSS", "React"]; let scores: number[] = [95, 87, 92]; // โโ Union Type โ แแแต แ แญแแต โโโโโโโโโโโโโโโโโโโโโโโโ let id: string | number = "abc123"; id = 42; // โ number แฐแแ OK แแ // โโ Function Types โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ function greet(name: string): string { return `แฐแแ! ${name}`; } function add(a: number, b: number): number { return a + b; } function logMsg(msg: string): void { console.log(msg); // void = แแแ แ แญแแแตแ } // โโ Optional Parameter โโโโโโโโโโโโโโโโโโโโโโโโโโ function hello(name: string, age?: number): string { return age ? `${name}, ${age}` : name; } hello("Abel"); // โ age แณแญแแญ OK hello("Abel", 25); // โ age แแฎ OK
Interface = Object แ
แญแฝ แญแแตแแ! User object name, email, age แซแตแแแแธแแ? Interface แซแจแแแฃแ! Type = Interface แฐแแณแณแญ แแ + Union/Intersection แญแฐแซแ!
Interface defines the shape of objects. TypeScript checks every object matches the interface โ no missing fields, no wrong types!
// โโ Interface โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ interface User { id: number; name: string; email: string; age?: number; // ? = optional role: 'admin' | 'user'; // literal union } // โ แตแญแญแ โ fields แแ แ แ const user: User = { id: 1, name: "Abel", email: "abel@ethiocode.com", role: "admin" }; // โ Error โ email แ แแทแ! // const bad: User = { id:2, name:"Sara" }; // โโ Interface Extending โโโโโโโโโโโโโโโโโโโโโโโโ interface Admin extends User { permissions: string[]; // User + extra fields } // โโ Type Alias โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ type Point = { x: number; y: number }; type Status = 'pending' | 'active' | 'done'; type ApiResponse<T> = { // Generic! success: boolean; data: T; error?: string; }; // โโ Function with Interface โโโโโโโโโโโโโโโโโโโโโ function createUser(data: User): User { return { ...data }; } // React แแตแฅ Props Interface: interface ButtonProps { label: string; onClick: () => void; disabled?: boolean; variant?: 'primary' | 'secondary'; }
React แ TypeScript แแญ แตแตแ แแ .tsx files แญแแแ! Props interface แตแฐแซแแ
โ Components type-safe แญแแแ! useState, useRef แแ typed แแธแ!
Use .tsx for React+TypeScript. Define Props interfaces โ TypeScript checks every prop passed to components!
import React, { useState } from 'react'; // โโ Props Interface โโโโโโโโโโโโโโโโโโโโโโโโโโโโ interface CounterProps { initial: number; step?: number; // optional, default 1 label: string; onCount?: (count: number) => void; } // โโ Typed Component โโโโโโโโโโโโโโโโโโโโโโโโโโโโ function Counter({ initial, step = 1, label, onCount }: CounterProps) { // useState typing โ number แฅแป! const [count, setCount] = useState<number>(initial); function increment(): void { const next = count + step; setCount(next); onCount?.(next); // Optional chaining } return ( <div> <h3>{label}: {count}</h3> <button onClick={increment}>+{step}</button> </div> ); } // โโ Usage โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ <Counter initial={0} step={5} label="EthioCode Score" onCount={(n) => console.log(`Score: ${n}`)} /> // โ Error โ initial แณแญแฐแฅ: // <Counter label="Test" /> // TS: Property 'initial' is missing!
3 Interactive demos! Type Checker โ value type แซแจแแแฃแแ | Interface Builder โ TS code แญแฐแซแแ | Error Finder โ TypeScript error แ แแ! ๐ท
Try all three โ see TypeScript in action! Check types, build interfaces, and find errors!
Value แปแ โ TypeScript แแ type แฅแแฐแแ แซแณแซแแ! Type แ แแญแญ โ TS แซแจแแแฅแแ!
Enter a value and see what TypeScript type it would be โ and if your declared type matches!
Interface แตแ แปแ โ Fields แจแแญ โ TypeScript code แญแฐแซแแ!
Build a TypeScript interface visually โ add fields and see the generated TS code!
JavaScript vs TypeScript โ แแ แแฉแแถแฝ:
| Feature | JavaScript | TypeScript ๐ท |
|---|---|---|
| Type checking | โ Runtime only | โ Compile time |
| Error detection | โ ๏ธ แแแญแถ / Late | โ แแตแ / Early |
| Autocomplete | ๐ก Basic | โ Excellent |
| Refactoring | ๐ Scary | โ Safe & Easy |
| Learning curve | โ แแแ / Easy | ๐ก แตแแฝ แซแตแธแแซแ |
| Big projects | ๐ฐ Messy | โ Manageable |
| Browser support | โ Direct | โ Compiles to JS |
| React / Node | โ Works | โ Works + safer |
แแแ JS code valid TS แแ! Type แจแแฎ แแตแ Error แซแณแซแแ! Browser TS แ แซแฐแซแ โ tsc compile แซแฐแญแแแ JS แแ!
TS compiles to JS. Browsers run JS. TS only exists at development time โ it's your safety net!
let name: string = "Abel" โ colon (:) แจ variable แตแ แ แแ type แญแปแแ!
Add types with colon notation. TypeScript infers types automatically most of the time!
interface User {'{'} name: string; age: number {'}'} โ Objects แตแญแญแแ fields แแธแ แซแจแแแฃแ!
Interfaces define the expected shape of objects โ missing or wrong fields = compile error!
Props interface แตแซ โ Component type-safe แญแแแ! useState<number>(0) โ generic type!
TypeScript makes React components self-documenting โ you know exactly what props are expected!
แตแแแฝ projects = JS OK! แกแตแ แแญแ แตแแ
project = TS แแแญ! npx create-react-app my-app --template typescript
Start with TypeScript on team projects or anything larger than a weekend hobby app!
Excellent! TypeScript แฐแแญแ โ Code แ แ แแซแซ แ แฐแจแแ! ๐
แแฃแญ: Next.js โ React Framework! โฌ๐
๐ ethiocodesoftselect.netlify.app
แปแแแ แญแแฑ โ Join ๐