TypeScript
Completed
-
Section 1: Getting Started
-
Section 2: Basic Types
-
Section 3: Functions and Objects
-
Section 4: Advanced Types
-
Section 5: Object-Oriented TypeScript
-
Section 6: Working with Modules
-
Section 7: Tooling and Practice
-
Section 8: Type-Level Programming
-
Section 9: TypeScript with Backends
-
Section 10: Testing Typed Code
-
Section 11: Data Validation with Types
-
Section 12: Practical Projects
-
Section 13: Compiler Internals
-
Section 14: Configuration Deep Dive
-
Section 15: Enums, Symbols, and Special Types
-
Section 16: Working with Async Code
-
Section 17: TypeScript and the DOM
-
Section 18: Advanced Generics Practice
-
Section 19: Working with Third-Party Types
-
Section 20: Monorepo and Large-Scale Practices
-
Section 21: Common Pitfalls and Best Practices
-
Section 22: Interview Practice
-
Section 23: Handbook: Narrowing In Depth
-
Section 24: Handbook: Object Types In Depth
-
Section 25: Handbook: Classes In Depth
-
Section 26: Handbook: Modules In Depth
-
Section 27: Handbook: Declaration Files In Depth
-
Section 28: JSX and Namespaces
-
Section 29: Compiler Configuration Reference
-
Section 30: More Practice Exercises
-
Section 31: Handbook: Everyday Types Deep Dive
-
Section 32: Utility Types Full Reference
-
Section 33: Decorators Reference
-
Section 34: Mixins and Advanced OOP Patterns
-
Section 35: Iterators and Generators Typing
-
Section 36: More Type-Level Programming Practice
-
Section 37: TypeScript Ecosystem Tools
-
Section 38: TypeScript with Testing Frameworks
-
Section 39: TypeScript for Library Authors
-
Section 40: More Interview Practice
-
Section 41: Handbook: Functions In Depth
-
Section 42: Handbook: Type Manipulation Deep Dive
-
Section 43: More Real-World Patterns
-
Section 44: TypeScript Release Notes Highlights
-
Section 45: Final Practice and Review
3: Configuring tsconfig.json
So, we've got the compiler installed and we're writing some types. But if you're like me, you've probably noticed that running tsc index.ts is fine for a tiny script, but it gets annoying fast. I'm currently working on a small Product Catalog utility, and my project folder is starting to look like a disaster zone because every .ts file is spitting out a .js file right next to it.
The Messy Root Folder Problem
Right now, my folder looks like this: product.ts, product.js, user.ts, user.js. It's cluttered. I want my source code in one place and my compiled code in another. I could try passing flags to the command line, but that's a nightmare to remember.
I'll run tsc --init. This generates a tsconfig.json file. It's huge—mostly comments—but it tells the compiler, "Hey, stop guessing and use these settings for every file in this project."
I'm going to find the outDir setting. By default, it's commented out. I'll uncomment it and set it to "./dist":
{
"compilerOptions": {
"outDir": "./dist"
}
}
Now, when I just run tsc (without specifying a filename), the compiler looks at this config file, sees the outDir, and magically sweeps all those .js files into a clean /dist folder. Much better. Now I can actually see my source code again.
Teaching TS Which Era of JS to Use
Here is where things get interesting. In my catalog.ts file, I'm using async/await and some arrow functions. If I'm deploying this to a very old environment—say, an ancient version of Node or an old browser—those features might crash the app.
I'll look for the target option. Right now it's likely set to ES2016 or ESNext. Let's see what happens if I force it to ES5 (the old school standard):
{
"compilerOptions": {
"outDir": "./dist",
"target": "ES5"
}
}
I'll run tsc again and peek into dist/catalog.js. Suddenly, my sleek arrow functions have been transformed into chunky function() {} declarations. TypeScript isn't just checking types; it's acting as a translator. I usually keep this at ES2020 or ESNext for modern projects, but knowing you can toggle this is a lifesaver when you're supporting legacy systems.
Turning Up the Volume on the Compiler
I noticed something frustrating. I wrote a function that takes a user object, and I'm accessing user.address.zipcode. Even though I defined the type, TypeScript is letting me get away with a potential crash if address happens to be undefined. It's being too "nice."
I want the compiler to be a drill sergeant. I'll find the strict flag and set it to true:
{
"compilerOptions": {
"outDir": "./dist",
"target": "ES2020",
"strict": true
}
}
The moment I save this, my editor lights up with red squiggles. TypeScript is now screaming at me: "Object is possibly 'undefined'". This is exactly what I want. By enabling strict, I'm opting into noImplicitAny and strictNullChecks. It's more work up front, but it's the only way to actually trust your types.
Defining the Boundaries
One last thing. I've got some helper scripts and a tests/ folder in my project. I don't want TypeScript trying to compile my test files into the dist folder; that's just wasted space and potential bloat.
I'll add include and exclude arrays at the root of the config file (outside of compilerOptions):
{
"compilerOptions": {
"outDir": "./dist",
"target": "ES2020",
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
Now, I've told the compiler: "Only look at files inside the src folder, and absolutely ignore anything in node_modules or any file ending in .spec.ts." My build process is now lean, predictable, and strict. This is the baseline for almost every professional TS project I've ever touched.
📋 Practical Task
Exercise: Hardening the Inventory System Config
You have been handed a project for an Inventory System, but the current tsconfig.json is too permissive, and the file structure is a mess. Your goal is to clean up the configuration to match professional standards.
The Current State:
- The project has a
src/folder for source code and atests/folder for unit tests. - The
tsconfig.jsoncurrently has"strict": falseand nooutDirdefined. - The
targetis currently set to"ES3", which is unnecessarily old for this modern Node.js project.
Your Requirements:
- Modify
tsconfig.jsonto ensure all compiled JavaScript files are sent to a folder namedbuild. - Update the
targettoES2020. - Enable
strictmode to catch potentialnullorundefinederrors. - Configure the compiler to only include files within the
srcdirectory and explicitly exclude thetestsdirectory.
Verification: Once you've updated the config, run tsc. Check that no .js files appear in src/ and that all output is contained within build/.
There are no comments for now.