Simplify complicated Typescript - KISS typescript

Simplify complicated Typescript - KISS

Simplifying complicated TypeScript with the KISS principle: refactoring examples show when explicit types improve a simple function and when clever type gymnastics make code harder to review, test, and maintain.

Feb 12, 2024

Advanced Switch Case Techniques in TypeScript

Advanced Switch Case Techniques in TypeScript

TypeScript switch statements can be more than a chain of cases: wrap the switch in a function for cleaner returns, or swap it for an object map for flat, readable branching. Each technique comes with code samples, pros, and cons so you can pick the right one.

Understanding Record type in Typescript

Understanding Record type in Typescript

The Record utility type in TypeScript builds object types with fixed keys and value types. A worked example shows Record<Keys, Type> with union keys: restricting IDs to chosen literals and mixing User and AdminUser values, so the compiler flags missing or extra entries.

Understanding TypeScript Function Generators

Understanding TypeScript Function Generators

TypeScript function generators can pause and resume execution, returning values one at a time instead of all at once. What generators are, how yield works in a simple example, and a practical number sum generator that shows where they beat regular functions.

How to deal with infinite scroll in React

How to deal with infinite scroll in React

Infinite scroll in React gets much easier with react-query: the useInfiniteQuery hook fetches posts page by page, cancels stale requests with AbortSignal, and exposes fetchNextPage for loading more. A complete typed example wrapped in a reusable usePosts hook.

Understanding generic types in Typescript

Understanding generic types in Typescript

Generic types in TypeScript let one function, class, or interface work safely with many data types. What generics are, how a type parameter beats hardcoding a concrete entity type in a validation helper, and honest pros and cons before you use them everywhere.