๐Ÿ”ท
ETHIOCODE ยท LESSON 19 ยท TYPESCRIPT
TYPE SAFETY ยท SUPERSET ยท NEXT LEVEL

// JavaScript with superpowers TypeScript Type-Safe JavaScript ยท Compile-Time Errors ยท Better Code

JavaScript แŠ• แŒ แŠ•แŠซแˆซ แˆแŠ•! Error แˆณแ‹ญแˆ†แŠ• แ‰แŒญ แ‰ฅแˆŽ แ‰€แ‹ตแˆž แ‹ซแˆณแ‹ซแˆƒแˆ! ๐Ÿ”ท
TypeScript catches bugs BEFORE you run your code โ€” like having a super-smart assistant reviewing every line!

JS
+
Types
=
TS ๐Ÿ”ท

A
Created by @AppMinds_ET
01๐Ÿ”ท
TypeScript แˆแŠ•แ‹ตแŠ• แАแ‹?// What & Why

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!

โŒ JavaScript โ€” Error Runtime แˆ‹แ‹ญ แ‰ฅแ‰ป แ‹ซแˆณแ‹ซแˆ (แ‹˜แŒแ‹ญแ‰ถ!)
1function add(a, b) { return a + b; }
2add(5, "hello"); โš ๏ธ Runtime: "5hello" โ€” Number + String! JS แ‰€แ‹ตแˆž แŠ แˆ‹แˆณแ‹ˆแ‰€แˆ!
3
โœ… TypeScript โ€” Compile แˆฒแˆ†แŠ• แ‰€แ‹ตแˆž แ‹ซแˆณแ‹ซแˆƒแˆ!
1function add(a: number, b: number): number {
2 return a + b; }
3add(5, "hello"); ๐Ÿ”ด TS2345: Argument 'string' not assignable to 'number' โ€” แ‰€แ‹ตแˆž แ‰†แˆ˜! โœ…
4add(5, 10); // โœ… 15
JavaScript (.js)
// แˆแŠ• type แАแ‹? แˆ›แŠ• แ‹ซแ‹แ‰ƒแˆ!
let name = "Abel";
let age = 25;
function greet(user) {
  return "Hi " + user.name;
}
// user แˆแŠ• fields แŠ แˆˆแ‹?
// แˆ›แŠ• แ‹ซแ‹แ‰ƒแˆ โ€” run แˆฒแˆ†แŠ•!
โŸถ
+types
TypeScript (.ts) ๐Ÿ”ท
// แˆแˆ‰แˆ clear แАแ‹!
let name: string = "Abel";
let age: number = 25;
interface User {
  name: string; age: number;
}
function greet(user: User): string {
  return "Hi " + user.name; }
02๐Ÿท๏ธ
Basic Types โ€” แ‹‹แŠ“ แŠ แ‹ญแАแ‰ถแ‰ฝ// Type System

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!

string
"Abel" | 'hello'
แŒฝแˆ‘แ / Text values
number
42 | 3.14 | -5
แ‰แŒฅแˆฎแ‰ฝ แˆแˆ‰ / All numbers
boolean
true | false
แŠฅแ‹แАแ‰ต/แˆแˆฐแ‰ต / True or false
string[]
["a","b","c"]
String array แ‹แˆญแ‹แˆญ
number[]
[1, 2, 3]
Number array แ‹แˆญแ‹แˆญ
object
{name:"Abel"}
แ‹•แ‰ƒ / Key-value pairs
void
return; (nothing)
แˆแŠ•แˆ แŠ แ‹ญแˆ˜แˆแˆตแˆ / No return
any
แˆแŠ•แˆ แ‹ญแˆ†แŠ“แˆ‰
โš ๏ธ TypeScript แŠ• แ‹ซแŒ แ‹แ‹‹แˆ!
A | B
string | number
Union โ€” 2+ types
types.ts โ€” Basic Types Examples
// โ”€โ”€ 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
03๐Ÿ“
Interface & Type// Object Shape Definition

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.ts โ€” Interface & Type แˆแˆณแˆŒ
// โ”€โ”€ 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';
}
04โš›๏ธ
TypeScript + React// .tsx files

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!

Button.tsx โ€” TypeScript React Component
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!
05๐ŸŽฎ
Live TypeScript Playground!// Interactive

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!

๐Ÿ”ท TypeScript Playground
tsc v5.3 ready
๐Ÿ” Type Checker
๐Ÿ“ Interface Builder
๐Ÿ› Error Finder

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!

Fields โ€” Interface Properties
โ† Interface แˆตแˆ แŒปแ โ†’ Generate แŒซแŠ•
โฌ‡๏ธ แ‹ญแˆ… TypeScript code แ‹แˆตแŒฅ Error แŠ แˆˆ! แ‹จแ‰ฑ line แˆ‹แ‹ญ แАแ‹? แˆแˆญแŒฅ!
Find the TypeScript error in the code below โ€” which line causes a type error?
06โš–๏ธ
JS vs TypeScript// When to Use What?

JavaScript vs TypeScript โ€” แ‹‹แŠ“ แˆแ‹ฉแАแ‰ถแ‰ฝ:

FeatureJavaScriptTypeScript ๐Ÿ”ท
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
07๐Ÿ“Œ
แ‹‹แŠ“ แ‹‹แŠ“ แАแŒฅแ‰ฆแ‰ฝ// Key Takeaways
1
๐Ÿ”ท TypeScript = JavaScript + Types

แˆแˆ‰แˆ 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!

2
๐Ÿท๏ธ Basic Types โ€” string, number, boolean, array

let name: string = "Abel" โ€” colon (:) แŠจ variable แˆตแˆ แ‰ แŠ‹แˆ‹ type แ‹ญแŒปแ‹แˆ!
Add types with colon notation. TypeScript infers types automatically most of the time!

3
๐Ÿ“ Interface โ€” Object แ‰…แˆญแŒฝ แ‹ญแ‹ˆแˆตแŠ“แˆ

interface User {'{'} name: string; age: number {'}'} โ€” Objects แ‰ตแŠญแŠญแˆˆแŠ› fields แŠ“แ‰ธแ‹ แ‹ซแˆจแŒ‹แŒแŒฃแˆ!
Interfaces define the expected shape of objects โ€” missing or wrong fields = compile error!

4
โš›๏ธ React + TS = .tsx files

Props interface แˆตแˆซ โ†’ Component type-safe แ‹ญแˆ†แŠ“แˆ! useState<number>(0) โ€” generic type!
TypeScript makes React components self-documenting โ€” you know exactly what props are expected!

5
๐Ÿš€ TS แŠ• แˆ˜แ‰ผ แŒ€แˆแˆญ?

แ‰ตแŠ“แŠ•แˆฝ 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!

08๐ŸŽฏ
Quiz// แˆแŠ• แ‰ฐแˆ›แˆญแŠ•?
โ“ TypeScript แ‹จแˆšแˆฐแˆซแ‹ แˆแŠ• แŒŠแ‹œ แАแ‹? Errors แˆฒแ‹ซแˆณแ‹ญ?
When does TypeScript catch errors โ€” at what stage?
A Browser แ‹แˆตแŒฅ run แˆฒแˆ†แŠ• โ€” Runtime
B Deploy แŠจแˆ†แА แ‰ แŠ‹แˆ‹ โ€” Production
C Compile แˆฒแˆ†แŠ• โ€” Code แˆฒแŒฝแ โœ…
D Test แˆฒแˆฐแˆซ โ€” Test time
๐Ÿ”ท แŠญแแˆ 19 โ€” TypeScript!

Excellent! TypeScript แ‰ฐแˆ›แˆญแŠ• โ€” Code แŠ• แŒ แŠ•แŠซแˆซ แŠ แ‹ฐแˆจแŒแŠ•! ๐ŸŽ‰

แ‰€แŒฃแ‹ญ: Next.js โ€” React Framework! โฌ›๐Ÿš€

๐ŸŒ ethiocodesoftselect.netlify.app

โœˆ @EthioCodeSoftSelect

แ‰ปแŠ“แˆ‰แŠ• แŠญแˆแ‰ฑ โ†’ Join ๐Ÿš€
โœ๏ธ Created by@AppMinds_ET