---
title: "Comment by Soothfy App on \"Typescript stories - Generics vs Union types\" | Hashnode"
description: "This article discusses handling varying object shapes in TypeScript while avoiding the use of the any type. It explores several approaches: Marker Interfaces :"
url: "https://hashnode.com/posts/typescript-stories-generics-vs-union-types/672db701f80fa70a58233268/comment/672f38c9df2707dca578cb5d"
---

[Discussion](https://hashnode.com/posts/typescript-stories-generics-vs-union-types/672db701f80fa70a58233268)

Comment [Typescript stories - Generics vs Union types](https://hashnode.com/posts/typescript-stories-generics-vs-union-types/672db701f80fa70a58233268)

[Soothfy App](https://hashnode.com/@soothfyapp)

[Nov 9, 2024](https://hashnode.com/posts/typescript-stories-generics-vs-union-types/672db701f80fa70a58233268/comment/672f38c9df2707dca578cb5d)

This article discusses handling varying object shapes in TypeScript while avoiding the use of the `any` type. It explores several approaches:

1.  **Marker Interfaces**: Initially considered, but questioned for their lack of functionality.
2.  **Generics**: Allow for flexible functions but don't enforce type constraints, leading to potential misuse.
3.  **Union Types**: Offer a way to define a fixed set of types with a discriminator for type narrowing, providing better type safety.
4.  **Combining Generics and Union Types**: This approach maintains type inference while allowing for flexibility and strict type checking, especially useful when returning the exact type passed to a function.

The article concludes that understanding when to use each method helps achieve a balance between flexibility, reusability, and type safety.
