
Solved: I’ve released a Biome plugin to prevent Typescript type assertions
🚀 Executive Summary TL;DR: TypeScript’s as keyword (type assertions) can lead to silent runtime errors by overriding the compiler’s type inference without validating data shape. This article presents safer alternatives like conditional checks and type guards, culminating in the new Biome noAsAssertion plugin to enforce these practices and prevent as assertions across a codebase. 🎯 Key Takeaways Type assertions ( as keyword) bypass TypeScript’s type checking, creating a ‘lie’ to the compiler that can result in runtime TypeError if the actual data shape differs. Type guards provide a robust, reusable, and type-safe mechanism to validate an object’s shape at runtime, allowing TypeScript to correctly infer types within conditional blocks without assertions. The Biome noAsAssertion plugin offers a ‘nuclear’ option to enforce the prohibition of as assertions across a project, integrating into CI/CD to prevent future introduction of this code smell. Tired of TypeScript’s as keyword causing ru
Continue reading on Dev.to Tutorial
Opens in a new tab

