TypeScript Without the Tears
TypeScript is just JavaScript with spell-check. Let me prove it.
TypeScript Without the Tears
Okay, TypeScript has a reputation for being confusing. I get it โ the first time you see type Props = { name: string; age: number } your brain goes "nope." But here's the thing: TypeScript is literally just JavaScript with spell-check. Let me show you.
Why Would You Use TypeScript? (Real Examples)
"But JavaScript already works, why add extra stuff?" Here's why real companies and developers switched and never looked back:
- Airbnb: They estimated that 38% of their bugs could have been prevented by TypeScript. They migrated their entire codebase and bug reports dropped significantly.
- Slack: Their desktop app was full of hard-to-find bugs from passing wrong data between components. TypeScript caught those errors instantly.
- Your editor gets superpowers: With TypeScript, VS Code can autocomplete property names, show you what a function expects, and underline errors as you type โ like Google Docs' grammar suggestions but for code.
- Team collaboration: When someone else writes a function, TypeScript tells you exactly what it expects and returns. No more guessing, no more reading through someone's code to figure out what data shape it needs.
- Refactoring without fear: Want to rename a field from
userNametodisplayName? TypeScript highlights every place in your entire codebase that needs to change. In JavaScript, you'd find out you missed one when a user reports a bug at 2am. - Job listings: The majority of frontend job postings now require or prefer TypeScript. It's not a "nice to have" anymore โ it's expected.
Basically: JavaScript lets you do whatever you want, which sounds nice until you realize "whatever you want" includes accidentally breaking your app. TypeScript gently stops you from shooting yourself in the foot.
The Core Idea
Have you ever written JavaScript and then spent 45 minutes debugging, only to realize you typed user.nme instead of user.name? JavaScript wouldn't tell you about that typo until your code actually runs and breaks.
TypeScript catches those mistakes BEFORE you run your code. It's like having a really helpful friend reading over your shoulder going "hey, did you mean 'name'?"
๐ฌ Denise says
I resisted TypeScript for months because it looked like extra work. Then I spent an entire Saturday debugging a bug that TypeScript would have caught in 0.2 seconds. I installed TypeScript on Monday. Learn from my mistakes.
Your First Type
In JavaScript, a variable can be anything:
1// JavaScript โ no rules, anything goes2let name = 'Denise';3name = 42; // JavaScript: 'Sure, whatever!'4name = true; // JavaScript: 'Go for it!'5name = [1, 2, 3]; // JavaScript: 'You do you!'67// TypeScript โ gentle guardrails8let name: string = 'Denise';9name = 42; // TypeScript: 'Hey, that is not a string!'10name = true; // TypeScript: 'Still not a string!'11name = 'Sarah'; // TypeScript: 'Perfect!'
The Basic Types
There are only a few types you need to know:
1// The types you will use 99% of the time:23let name: string = 'Denise'; // Text4let age: number = 28; // Numbers (whole or decimal)5let isAwesome: boolean = true; // true or false6let hobbies: string[] = ['coding', 'coffee', 'cats']; // Array of strings7let nothing: null = null; // Intentionally empty89// But honestly? TypeScript can figure most of these out:10let name = 'Denise'; // TypeScript KNOWS this is a string11let age = 28; // TypeScript KNOWS this is a number12let isAwesome = true; // TypeScript KNOWS this is a boolean1314// You only need to write types when TypeScript can't guess
๐ก Pro tip
You don't need to type EVERYTHING. Let TypeScript's inference do the heavy lifting. Only add explicit types when TypeScript can't figure it out on its own, or when you want to be extra clear about what a function expects.
Objects: Describing the Shape of Data
This is where TypeScript really shines โ describing what your data looks like:
1// Define the shape of a User2type User = {3name: string;4email: string;5age: number;6isPro: boolean;7};89// Now TypeScript knows exactly what a User looks like10const denise: User = {11name: 'Denise',12email: 'hello@techwithdenise.com',13age: 28,14isPro: true,15};1617// Try to make a bad User? TypeScript stops you:18const broken: User = {19name: 'Oops',20// TypeScript: 'Hey! You are missing email, age, and isPro!'
Functions with Types
Tell TypeScript what goes in and what comes out:
1// What goes in: name (string) and age (number)2// What comes out: a string3function introduce(name: string, age: number): string {4return 'Hi, I am ' + name + ' and I am ' + age + '!';5}67introduce('Denise', 28); // Works!8introduce('Denise'); // Error: missing 'age' argument9introduce(28, 'Denise'); // Error: arguments are in wrong order!1011// Optional parameters โ add a ? to make it optional12function greet(name: string, emoji?: string): string {13return 'Hey ' + name + (emoji ? ' ' + emoji : '!');14}1516greet('Denise'); // 'Hey Denise!'17greet('Denise', 'โจ'); // 'Hey Denise โจ'
TypeScript with React
If you're using React (which you probably are if you're here), TypeScript makes props way safer:
1// Define what props your component accepts2type ProfileCardProps = {3name: string;4role: string;5emoji: string;6isOnline?: boolean; // Optional!7};89function ProfileCard({ name, role, emoji, isOnline }: ProfileCardProps) {10return (11<div>12<span>{emoji}</span>13<h2>{name}</h2>14<p>{role}</p>15{isOnline && <span>Online now</span>}16</div>17);18}1920// TypeScript will now help you everywhere you use this:21<ProfileCard name='Denise' role='Creator' emoji='๐' /> // Perfect!22<ProfileCard name='Denise' /> // Error: missing role and emoji!23<ProfileCard name='Denise' role='Creator' emoji='๐' color='pink' /> // Error: color is not a valid prop!
The Migration Path
You don't have to convert your whole project at once. Here's how to ease into it:
- Rename
.jsfiles to.ts(or.jsxto.tsx) - Fix the errors TypeScript shows you (usually just adding a few types)
- Add types to your functions and components gradually
- Enjoy never having "undefined is not a function" ever again
โ ๏ธ Heads up
Don't try to make everything "perfectly typed" on day one. Start with any for things you're unsure about (it turns off type checking for that variable) and gradually replace any with real types as you get more comfortable. Progress over perfection.
Quick Recap
- TypeScript = JavaScript + spell-check for your code
- Basic types:
string,number,boolean,string[] - TypeScript infers most types โ you don't need to type everything
typecreates a blueprint for objects- Add
?to make things optional - In React, type your props for autocomplete and error catching
- Migrate gradually โ you don't need to do everything at once
๐ฌ Denise says
TypeScript's whole job is to have your back. It's not there to make your life harder โ it's there to catch the bugs you'd spend hours finding on your own. Give it a real chance (not just one frustrated afternoon) and I promise you'll wonder how you ever coded without it. Every major company uses TypeScript now, and that's not a coincidence.
Photo coming soon โจ
Want to keep going?
Tell me what you want to build next and I'll help you write the code.
Start Building โจ