🚀 Understanding Generics in TypeScript (A Powerful Concept) While building my small utility library recently, I explored one of the most powerful features in TypeScript: Generics. 💡 What are Generics? Generics allow us to write reusable and type-safe code that works with multiple data types without losing type information. Instead of using any, we can create flexible functions that still maintain strong typing. Example without Generics: function first(arr: any[]) { return arr[0]; } Here TypeScript cannot infer the return type properly. Now with Generics: function first<T>(arr: T[]): T { return arr[0]; } Now TypeScript automatically understands the type. Example: first([1,2,3]) // number first(["a","b"]) // string 🎯 Why Generics are useful • Reusable code • Strong type safety • Better autocomplete in editors • Cleaner library APIs Many popular libraries like React, Redux, and TanStack Query rely heavily on Generics. While working on my small utility library, I used generics in functions like: function chunk<T>(arr: T[], size: number): T[][] This allows the function to work with numbers, strings, objects, or any type while preserving type safety. 📚 Learning TypeScript deeply really changes how you think about designing APIs and reusable code. Still exploring more advanced TypeScript concepts like: Utility Types Conditional Types Mapped Types Always learning 🚀 #typescript #webdevelopment #javascript #softwareengineering #100daysofcode #programming #coding
TypeScript Generics Explained
More Relevant Posts
-
🚀 TypeScript is about to get 10x faster — and the last JS-based version just dropped its RC Big news this week for every TypeScript developer. 👀 TypeScript 6.0 RC landed on March 6. GA drops on March 17. But here's the real story - this is the last TypeScript version written in TypeScript. After 6.0, everything changes. 🔥 What's happening? Microsoft is rewriting the entire TypeScript compiler in Go (yes, Go 🐹) with promises: ~10x faster builds Near-instant incremental compilation ~50% memory reduction Much faster editor startup What's new in 6.0 itself? RegExp.escape - finally, safe regex escaping built-in New Temporal API types (ES2026 is coming 🎉) getOrInsert / getOrInsertComputed for Map & WeakMap Subpath imports starting with #/ asserts keyword deprecated → use with instead Better type inference for generic function expressions What should you do now? Install the RC → npm install -D typescript@rc Run with --deprecation flag to catch anything that'll break in 7.0 Start thinking about your build pipeline - things will change 6.0 is the bridge. 7.0 is the destination. The TypeScript team has been quietly heads-down on this rewrite for over a year. The speed gains are real - already tested on large codebases. Are you excited about the Go-powered future of TypeScript, or does rewriting a compiler in a different language make you nervous? 👇 #TypeScript #JavaScript #WebDevelopment #Frontend #SoftwareEngineering #DeveloperTools #NodeJS #Programming #TechNews #OpenSource
To view or add a comment, sign in
-
-
TypeScript 6.0 is here: The end of an era (and a massive speed boost) ⚡ If you’ve been using TypeScript, things are about to change—in a very fast way. The latest update (v6.0) isn't just a regular patch. It's the "final warning" before TypeScript completely rewrites its core. The team is moving away from its original JavaScript codebase to a brand-new compiler written in Go. Why does this matter? Because we’re talking about native speed and multi-threaded type checking. Here is the ELI5 (Explain Like I'm 5) on what’s changing: </> The "Go" Migration: TypeScript is graduating. By moving its compiler to Go, future versions will be significantly faster at checking your code. </> Smart Defaults: Strict mode is now ON by default. It's TypeScript’s way of saying "I’m not asking anymore—write safer code!" 🛑 </> Cleaning the Closet: They are removing a lot of "old" features (like ES5 support and certain module resolutions) to make the engine leaner and meaner. </> Faster Installs: A change in how types are handled in node_modules could speed up projects by 20–50%. The Takeaway: We are moving toward a world where "compilation time" might finally stop being a coffee-break excuse. TypeScript is getting serious about performance. Are you excited about the move to a Go-based compiler, or do you think the "breaking changes" will be a headache for large codebases? #TypeScript #WebDev #SoftwareEngineering #GoLang #ProgrammingNews #TechTrends
To view or add a comment, sign in
-
-
Why TypeScript is non-negotiable for Scalable APIs. The "510" error is why I stopped writing pure JavaScript for production. We’ve all seen it. You expect a sum of 5 + 10 = 15, but instead, you get "510" because JavaScript decided to concatenate a string instead of adding numbers. In a small script, that’s a "funny" bug. In a mission-critical backend service, that’s a production incident. This is why TypeScript is not "nice to have" in the NestJS ecosystem but it’s an essential requirement. When you use TypeScript, you’re not just adding types; you’re building a contract for your data. Why it matters for your team: - Compile-time safety: Catch the "510" error at 2:00 PM on your machine, not at 2:00 AM in your logs. - Self-Documenting Code: When you hover over a function, you know exactly what it needs and what it returns. No more guessing what data contains. IDE Superpowers: IntelliSense, safe refactoring, and auto-completion make your team 2x faster. - TypeScript moves your debugging to the earliest possible stage. As a Senior Engineer, my job isn't to write code faster; it's to write code that stays broken for the least amount of time. Do you still feel the "pain" of debugging runtime type errors in your current stack? Let's talk about how to solve it. #TypeScript #JavaScript #NestJS #SoftwareEngineering #CodeQuality #DeveloperExperience
To view or add a comment, sign in
-
-
Is TypeScript a separate language? I’m curious, how do you answer this question for yourself? In the meantime, I’ll share my opinion On one hand, TS exists exclusively within the JavaScript ecosystem. It follows ECMAScript standards, updates alongside them, and always compiles into JS regardless of what tool you use to compile your code But there is a counterargument, JS itself eventually turns into bytecode, just like any other programming language If we look at it very simply 🔸 TypeScript -> JavaScript -> ... -> Bytecode / machine code 🔹 JavaScript -> ... -> Bytecode / machine code And this is the same for any language. Does this extra step really change the status of the language? In my opinion, not really So why do I still believe it’s not a separate programming language? For me, the deciding factor is that TypeScript brings nothing new to the runtime Basically, we get typing for JS. TypeScript doesn’t have its own runtime, its own memory management, or its own optimization methods. It doesn't even have its own data structures, except for Enums, but they have a questionable reputation TypeScript is an incredible tool that fundamentally improves the development experience and takes JS to a new level. However, it is too closely tied to JavaScript and lacks enough unique features to be considered a separate programming language What do you think? Is TypeScript a full-fledged language or just a useful tool for JavaScript? #typescript #javascript #techthoughts
To view or add a comment, sign in
-
-
TypeScript 6.0 just shipped - and it's designed to be thrown away. This is the last release built on the JavaScript codebase. TypeScript 7.0, written in Go with native code and multi-threading, is already in preview and expected within months. So why release 6.0 at all? Because it's a bridge. Most of its changes exist to align your codebase with 7.0's architecture before the native port arrives. 𝗪𝗵𝗮𝘁'𝘀 𝗰𝗵𝗮𝗻𝗴𝗶𝗻𝗴: • strict is now true by default • types defaults to [] instead of auto-including everything in node_modules/@types • target: es5 is deprecated (ES2015 is the new floor) • baseUrl no longer acts as a module resolution root • --moduleResolution node (node10) is deprecated The types change alone improved build times by 20-50% in projects Microsoft tested. 𝗪𝗵𝗮𝘁'𝘀 𝗻𝗲𝘄: • Built-in types for Temporal API (stage 4) • Map.getOrInsert() and getOrInsertComputed() support • RegExp.escape() for safer regex construction • Subpath imports starting with #/ • --stableTypeOrdering flag to preview 7.0's deterministic type sorting To be fair, this release feels less about new capabilities and more about preparing for what's next. The TypeScript team is essentially asking: "Can you handle these deprecations now, so 7.0 doesn't break your build later?"
To view or add a comment, sign in
-
-
TypeScript 6.0 is here — but it’s not what you expect 👀 This isn’t just another version update of TypeScript. It’s a transition point. In fact, TypeScript 6.0 will be the last version built on the current JavaScript-based compiler — acting as a bridge to TypeScript 7.0, which is being rewritten in Go for massive performance improvements. --- ⚙️ What’s new (and important)? • Improved type checking → catches more bugs early • Better type inference → smarter and cleaner code • Support for modern JS features (like es2025) • New standard APIs (e.g., Temporal, RegExp updates) • --stableTypeOrdering → smoother migration to future versions --- 💡 But here’s the real story: TypeScript 6.0 is less about features… and more about preparing developers for the future. It’s aligning: ✔ modern defaults ✔ stricter type systems ✔ cleaner configurations So that upgrading to TypeScript 7.0 becomes seamless. --- 🚀 Why this matters The next version (TS 7.0) is expected to bring: • Faster builds • Better scalability • Native performance (Go-based compiler) Meaning: Your dev experience is about to get significantly faster. --- 📌 Takeaway TypeScript 6.0 isn’t flashy. It’s foundational. Sometimes the most important updates are the ones that prepare you for what’s coming next. --- Are you planning to upgrade early or wait for 7.0? 👇 #TypeScript #JavaScript #WebDevelopment #Frontend #Backend #Programming #SoftwareEngineering JavaScript Mastery TypeScript
To view or add a comment, sign in
-
-
TypeScript 6.0 just mass-deprecated half the configs most projects rely on. strict is now true by default. target is now es2025. module is now esnext. types defaults to an empty array. That last one is the silent killer. Previously TypeScript scanned every @types package in your node_modules automatically. Now it includes nothing. The moment you upgrade you will see: "Cannot find name 'fs'. Do you need to install type definitions for node?" The fix takes 10 seconds. Add "types": ["node"] to your tsconfig. But here is the part nobody is talking about yet. The TypeScript team says projects that set their types explicitly saw 20 to 50 percent faster builds. Your old config was silently wasting time on every single compilation. The deprecations go deeper than defaults. ES5 targets? Gone. --moduleResolution node? Deprecated. --baseUrl as a lookup root? Deprecated. --outFile? Deprecated. esModuleInterop: false? Cannot even set it anymore. These are not warnings. They are errors. You can add "ignoreDeprecations": "6.0" as a bandaid. But TypeScript 7.0 is months away — rewritten in Go — and it removes everything on this list permanently. There is good news too. Temporal API types are built in. No more date-fns for basic date math. Map.getOrInsert replaces the check-and-set boilerplate pattern in a single line. RegExp.escape finally exists natively. And there is a --stableTypeOrdering flag that lets you test your codebase against TypeScript 7.0 output right now. Before it ships. The teams that migrate today will have a painless upgrade to 7.0. The teams that wait will hit a wall. I wrote a full technical breakdown with 15 code examples and a 12-step migration checklist here - https://lnkd.in/eeyq4PxS. #TypeScript #WebDevelopment
To view or add a comment, sign in
-
𝗧𝗮𝗸𝗲 𝗬𝗼𝘂𝗿 𝗧𝘆𝗽𝗲𝗦𝗰𝗿𝗶𝗽𝘁 𝗦𝗸𝗶𝗹𝗹𝘀 𝗧𝗼 𝗧𝗵𝗲 𝗡𝗲𝘅𝘁 𝗟𝗲𝘃𝗲𝗹 You know how TypeScript helps you catch typos and prevents errors. But many developers stop there. They treat TypeScript as just JavaScript with type annotations. The real power of TypeScript lies in its advanced type system. It helps you make invalid states unrepresentable. This means you can write code where the compiler catches logical errors before they become bugs. Let's explore some advanced types: - Union types allow a variable to be one of several types. - Discriminated unions create self-documenting, resilient code. - Template literal types generate new string literal types by combining others. - Utility types like Partial, Pick, and Omit help with type transformation. These types help you: - Create precise formats - Validate routes and function names at compile time - Build flexible, yet strict, abstractions - Update entities safely Newer operators like satisfies and as const help you infer and validate types with minimal annotation. Your challenge: Look at a key interface or state object in your project. Ask yourself: "Can this be in an inconsistent state?" If yes, refactor it using a discriminated union. Share your approach to using TypeScript's type system to prevent bugs in the comments below! Source: https://lnkd.in/gincVBvR
To view or add a comment, sign in
-
After 7 years of writing TypeScript, I still see the same pattern trip up even experienced devs: over-relying on `any` as an escape hatch instead of modeling uncertainty properly. The fix is usually one of these three: 1. Use `unknown` instead of `any`. It forces you to narrow the type before using the value. Safer, and it documents intent. 2. Use discriminated unions when a value can be one of several shapes. The compiler will catch exhaustiveness gaps for you at compile time, not at 2am in production. 3. Use template literal types for stringly-typed APIs. Instead of `string`, write `${string}-id` or a union of valid strings. Suddenly your editor catches typos you never would have caught in review. The shift I had to make mentally was treating TypeScript not as a linter on top of JS, but as a way to encode business rules in the type system itself. Once you do that, the compiler becomes your senior engineer who never sleeps. Still learning this every day. What's a TypeScript pattern that changed how you think about code? Drop it in the comments. #TypeScript #JavaScript #WebDevelopment #FullStack #SoftwareEngineering
To view or add a comment, sign in
-
Recently, I developed several tools for the TypeScript and JavaScript ecosystem, inspired by challenges I encountered as a developer. These tools may be beneficial for others, particularly those working with large codebases or migrating legacy code to newer frameworks. 1️⃣ Hardcoded Secret Scanner This tool scans your codebase for hardcoded API keys and secrets before they reach production. GitHub: https://lnkd.in/dyBqUY4w 2️⃣ Unused Code Finder It detects unused functions, variables, and exports across your project and can also clean them automatically. GitHub: https://lnkd.in/d5x33tyD 3️⃣ Dotenv Type Generator This tool reads your .env file and generates TypeScript types for process.env. GitHub: https://lnkd.in/dHquTsbT 4️⃣ TypeScript Mock Builder It creates fully typed mock objects from TypeScript interfaces to speed up testing. GitHub: https://lnkd.in/dMpXbt4N Additionally, I have included tools for complexity reports, naming checks, dependency graphs, and other project insights. Explore more here: https://lnkd.in/dW9r-UB6 If you have suggestions, ideas for improvements, or encounter any bugs, please feel free to open an issue on GitHub. #OpenSource #TypeScript #NodeJS #DeveloperTools
To view or add a comment, sign in
More from this author
-
📘 What I Learned About the Node.js fs Module (While Executing Code on the Server) Today, I spent time deeply understanding the Node.js fs (File Syst
Prince sah 3mo -
🚀 From Idea to Execution: Building an AI-Powered Code Editor (With a Real Compiler) Most code editors today help you write code. But what if your
Prince sah 3mo
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development