Monday, April 21, 2025

Show HN: I made TypeScript's type inference more strict (and smarter) https://ift.tt/nkSy5vH

Show HN: I made TypeScript's type inference more strict (and smarter) As a TypeScript developer, I often found myself wishing the type system could do more—*especially when omitting or modifying deeply nested properties* inside complex objects and arrays. For instance, what if I want to remove a deeply nested field like `user.profile.email` and also something like `user.posts[ ].meta.shares` from a type? TypeScript doesn't really provide a built-in way to do that. So I built *DeepStrictTypes* — a utility that lets you *omit deeply nested keys*, even inside arrays, with full type inference and strictness. Here’s an example: ```ts type Example = { user: { id: string; profile: { name: string; age: number; email: string; }; posts: { title: string; content: string; meta: { likes: number; shares: number; }; }[]; }; }; // Remove 'user.profile.email' and 'user.posts[ ].meta.shares' type Omitted = DeepStrictOmit< Example, 'user.profile.email' | 'user.posts[*].meta.shares' >; ``` The resulting type: ```ts { user: { id: string; profile: { name: string; age: number; }; posts: { title: string; content: string; meta: { likes: number; }; }[]; }; } ``` Works great for: - Cleaning up types for API responses - Dynamically transforming deeply nested data - Improving type safety when handling structured JSON [ https://ift.tt/sVbdRLX ]( https://ift.tt/sVbdRLX ) Would love your feedback or ideas for improvements! https://ift.tt/UsAJzxn April 22, 2025 at 07:47AM

No comments:

Post a Comment