Full-Stack Development · Tutorial 6 of 9
Fix Common TypeScript Errors for Beginners
By the FUTEAC Team · 16 November 2026 · 14 min read · Beginner
In short
TypeScript errors are the compiler telling you, before the page ever loads, that a value is not the type the code expects. The six errors here cover almost every red squiggle a beginner meets: a wrong type, a missing property, a wrong argument, an unknown name, a value that might be undefined, and a missing annotation.
- TypeScript checks your code before it runs. A red error is a bug report, not a punishment. Fix the code, not the checker.
- Every error code starts with TS and a number. Search the number, not the whole sentence, when you need help. TS2322 will always mean the same thing.
strict: trueturns on the checks that catch real bugs, including possibly-undefined values and implicitany. Keep it on, even when it feels strict.- The fastest fix is usually the smallest one: change the value, add the missing property, or add the missing type.
- This tutorial feeds the React from Scratch course (/course/react-from-scratch) and the TypeScript Basics (/skill/typescript-basics) skill.
Before you start
What is TypeScript, in one paragraph?
TypeScript is JavaScript with types written down. You write the same code, plus short notes about what each value is: this one is a number, that one is a string, this function takes a name and returns a greeting. The TypeScript compiler, tsc, reads those notes and checks them before the code runs. Think of it as a spellchecker that also checks your facts. A normal spellchecker flags a misspelled word. TypeScript flags a value used as the wrong kind of thing, a property that does not exist, or a name that was never declared. The check happens on your computer, in seconds, while the fix is still cheap.
How do you set up a practice folder?
You need one folder, TypeScript installed in it, and a short config file. Run these commands in your terminal:
mkdir ts-errors
cd ts-errors
npm init -y
npm install -D typescriptCheck the install worked:
npx tsc --versionYou should see:
Version 5.9.3Now create a file called tsconfig.json in the ts-errors folder. This is the whole file:
{
"compilerOptions": {
"target": "ES2020",
"strict": true,
"noEmit": true
},
"include": ["src"]
}Three settings matter here. target: ES2020 says which JavaScript the compiler aims at. strict: true turns on the full set of checks, including the two errors in sections 7 and 8 below. noEmit: true tells tsc to check only, and not write JavaScript files yet. Create the src folder, and you are ready.
From here on, each section puts one small file in src, runs the checker with npx tsc --noEmit, reads the error, and fixes it.
Error 1: TS2322, Type 'string' is not assignable to type 'number'
Put this in src/age.ts:
const age: number = "twenty-five";
console.log(age);Run npx tsc --noEmit. You get:
src/age.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.The (1,7) points at line 1, column 7, which is the name age. Read the message as a sentence: "You promised this would be a number, then handed it a string." The annotation : number is the promise, and "twenty-five" breaks it.
The fix depends on what you meant. If the age really is a number, change the value:
const age: number = 25;
console.log(age);If the text was the intent, change the promise instead:
const age: string = "twenty-five";Our rule: when the type is right and the value is wrong, change the value. Changing the annotation to fit a wrong value just moves the bug to the next line that uses it.
Error 2: TS2339, Property 'email' does not exist
Put this in src/user.ts:
const user = { name: "Ayesha", age: 24 };
console.log(user.email);Run the checker:
src/user.ts(2,18): error TS2339: Property 'email' does not exist on type '{ name: string; age: number; }'.TypeScript builds the shape of user from the object you wrote. That shape has name and age, and nothing else. Asking for user.email is asking for a property the shape does not have. The most common cause is exactly this: the property was never added. The second most common cause is a typo, user.emial instead of user.email, which produces the same error.
The fix is to add the property where the object is made:
const user = { name: "Ayesha", age: 24, email: "ayesha@example.com" };
console.log(user.email);The checker passes, and running the file prints ayesha@example.com. Notice where the fix went: at the object, not at the line that reads it. When a property is missing, the object is wrong, not the code that uses it.
Error 3: TS2345, Argument of type 'string' is not assignable to parameter of type 'number'
Put this in src/double.ts:
function double(n: number) {
return n * 2;
}
console.log(double("4"));Run the checker:
src/double.ts(4,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.The function asked for a number, and the call passed the string "4". This one confuses beginners because plain JavaScript would run it fine: double("4") prints 8, since JavaScript quietly converts the string. TypeScript refuses to guess. It treats "4" as text, and text is not a number, even when it looks like one.
The fix is to pass a number:
console.log(double(4));Running it prints 8. If the value really arrives as a string, from an input box or a web address, convert it openly instead of hiding the conversion:
console.log(double(Number("4")));Number("4") says, in the code, "I know this is text, turn it into a number." That is the honest version of what JavaScript was doing silently.
Error 4: TS2304, Cannot find name 'totalPrice'
Put this in src/total.ts:
console.log(totalPrice);Run the checker:
src/total.ts(1,13): error TS2304: Cannot find name 'totalPrice'.There is no trick here. The name was never declared in this file, so the checker has nothing to point at. Two things cause it in practice: you forgot to declare the variable, or you declared it with a different spelling, totalprice or total_price, and used another spelling here.
The fix is to declare it before you use it:
const totalPrice = 1200;
console.log(totalPrice);Running it prints 1200. If you did declare it and still see this error, check the spelling letter by letter, and check that the declaration is in the same file and above the line that uses it. A variable declared inside a function does not exist outside that function.
Error 5: TS18048, 'word' is possibly 'undefined'
Put this in src/first-letter.ts:
function firstLetter(word: string | undefined) {
return word.toUpperCase();
}
console.log(firstLetter("hello"));Run the checker:
src/first-letter.ts(2,10): error TS18048: 'word' is possibly 'undefined'.The type string | undefined means word can be a string or it can be undefined. The checker knows undefined has no toUpperCase method, so it stops you. This error only appears with strict: true. Without strict mode, the code compiles, and then it crashes at runtime the first time someone calls firstLetter(undefined) with "Cannot read properties of undefined". The strict check moves that crash from your user back to your editor, which is the whole point of it.
The fix is a guard: check before you use it.
function firstLetter(word: string | undefined) {
if (word === undefined) {
return "";
}
return word.toUpperCase();
}
console.log(firstLetter("hello"));After the if, the checker knows word is a string, so toUpperCase() is safe. Running it prints HELLO. The pattern is always the same: a check first, then the code that needs the value.
Error 6: TS7006, Parameter 'name' implicitly has an 'any' type
Put this in src/hello.ts:
function greet(name) {
return "Hello " + name;
}
console.log(greet("Ayesha"));Run the checker:
src/hello.ts(1,16): error TS7006: Parameter 'name' implicitly has an 'any' type.The parameter has no type annotation, so the checker has to guess, and under strict it refuses. any means "any type at all", which means no checking happens for that value. The error is telling you that you left a hole in the safety net.
The fix is one annotation:
function greet(name: string) {
return "Hello " + name;
}
console.log(greet("Ayesha"));Running it prints Hello Ayesha. Every function parameter needs a type. You will write hundreds of these annotations, and after the first week you will stop noticing them, the way you stop noticing full stops.
The wrong turn: silencing the checker with `any`
Here is the wrong turn every beginner takes at least once, and this tutorial would be dishonest if it skipped it. When TS2322 appears, the tempting fix is to change the annotation to any:
const age: any = "twenty-five";
console.log(age + 1);The checker passes. No red squiggle, no error, nothing to fix. Then you run the file, and Node prints:
twenty-five1age + 1 joined the text "twenty-five" with the number 1 and made a longer string, because age was text all along and any told the checker not to look. The bug did not go away. It moved from compile time, where the fix costs ten seconds, to runtime, where your user finds it. The checker is not the enemy. The error was the report, and any is shredding the report. If you catch yourself typing any to make an error vanish, stop and fix the value or the type instead.
How to verify the fix worked
Fix all six files, then run the checker once more:
npx tsc --noEmitWhen the code is clean, tsc prints nothing at all and exits with code 0. Check the code with:
echo $?You should see 0. No news is good news: Unix tools stay quiet when there is nothing to report, and a silent tsc is a passing tsc.
As a second check, run the fixed examples. The fixed files print 25, ayesha@example.com, 8, 1200, HELLO, and Hello Ayesha, which are the values the code promised. If any output surprises you, the code still has a bug, and the checker is the fastest way to find it.
Try it yourself
1. Write a function add(a: number, b: number) that returns a + b. Call it once with two numbers, then call it once with a string on purpose. Read the error code and fix the call. 2. Open tsconfig.json and change strict to false. Run npx tsc --noEmit and watch the TS18048 and TS7006 errors vanish. Change it back to true before you do anything else. That is what you give up when you turn strict off. 3. Take one file from your own project, or from the React from Scratch course, and rename it from .js to .ts. Run npx tsc --noEmit on it and fix the first error it reports.
Common mistakes
- Reaching for
anyto make the red go away. The error disappears, the bug moves to runtime, and your user finds it. Fix the value or the type instead. - Turning off
strictinstead of fixing the code. Strict mode is what catches the possibly-undefined and implicit-any errors. Turning it off trades a small fix now for a crash later. - Changing the annotation to fit a wrong value. Writing
const age: string = "twenty-five"when the code later doesage + 1just moves the error to the next line. Fix the value when the type is right. - Reading only the error code and skipping the message. The message names the exact value and the exact type it expected. Read the whole line before you search for help.
- Copying a fix from a search result without checking the number. TS2322 and TS2345 look alike, and their fixes are different. Match the number first, then the fix.
Frequently asked questions
Do I have to use TypeScript to learn React?
No. React works in plain JavaScript, and the React from Scratch course uses JavaScript throughout. TypeScript is the safety net you add when your app grows past a few files, or when you join a team that already uses it. This tutorial is the bridge for that moment.
What does the number in TS2322 mean?
It is just an ID for that error. TS2322 always means a value was given the wrong type, TS2304 always means a name was not found. Search the number when you need help, and you will get answers for your exact error instead of someone else's.
Why does tsc print nothing when my code is fine?
Because there is nothing to report. Command-line tools stay quiet on success. Silence plus exit code 0 is the pass mark, not a broken install.
Should I use any while I am learning?
Only as a last resort, and never as a habit. If a value truly can be anything, write the check that narrows it down, the way section 7 narrows undefined with an if. A file full of any is JavaScript with extra steps.
Does TypeScript slow down my app?
No. The types are erased when the code compiles, so the JavaScript that ships to the browser has no types in it at all. You pay the cost once, at check time, and your users pay nothing.
Follow the Full-Stack Developer Roadmap
Build complete web applications, frontend, backend APIs, databases and deployment. Free, step by step, at your pace.
View the Roadmap