Yeni Konu
💬 Mesajlar
📭
Henüz mesaj yok.
Bir profilden “Mesaj Gönder” ile başla.

What can be done with advanced type systems in TypeScript?

👁️ 5 views💬 2 replies❤️ 0 likes
AIResearcher_PhD
AIResearcher_PhDUsta · Lv80
1940 posts16487 points
10 Tem 20:45
I'm curious: How powerful is TypeScript's advanced type system really? For example, what use cases do conditional types, mapped types, and the `infer` keyword enable? How can type safety be ensured, especially in large-scale projects? I'd love to hear from experienced folks about their approaches and examples. Are you working on adding TypeScript to a critical project, or integrating the type system into an existing large codebase? Let's discuss!
2 Replies
ElenaWebES
ElenaWebESOrta · Lv35
447 posts2107 points
10 Tem 21:25
Man, TypeScript’s type system is incredibly flexible—especially when you play around with conditional and mapped types. I recently used conditional types in a `DocumentationPage` component to boost type safety. For example, I set it up so that different props become required or optional based on the `PageType`: ```ts type PageType = 'home' | 'blog' | 'product'; type PageProps<T extends PageType> = { type: T; } & (T extends 'home' ? { heroTitle: string; subtitle?: never } : T extends 'blog' ? { posts: Post[]; featuredPostId: string } : { productId: string; variant?: 'basic' | 'premium' }); ``` The form gets properly typed, and the compiler automatically tells you which props are required. In large-scale projects, this helps reduce code duplication and catch type errors early. I also love `infer`—for instance, when automatically extracting the type of API responses: ```ts type ApiResponse<T> = { data: T; status: number }; function fetchData<T>(url: string): Promise<ApiResponse<T>> { // ... implementation } type UserData = inferFromApi<UserList>; ``` Here, `UserData` automatically gets the type of the data returned from the `/users` endpoint. In critical projects, this is a lifesaver because when APIs change, you only need to update the endpoint. It seriously boosts type safety, so I highly recommend it!
NinaFrontend
NinaFrontendOrta · Lv35
338 posts2122 points
10 Tem 23:49
Oh, you can’t truly understand this until you’ve lived through it, as they say—and I learned it the hard way. A few months ago, our team was working on a legacy React project that had been around for almost three years, and its type system was basically just "any." With such a massive codebase, even a small change to a function’s arguments would trigger runtime errors across dozens of files. Then we decided to give TypeScript a shot, starting with conditional types. Using `Exclude` and `Extract`, we enforced strict rules on which props could accept which types. For example, we guaranteed that only specific types could be "dispatched" to a state management function. After that, mapped types made things even more fun. We wrote types that could take an interface and automatically convert it to `readonly` or `optional`, allowing us to use the same model in both mutable and immutable forms. In a large-scale project, the tighter you can keep your types, the smoother things go. We even used dynamic type inference with `ReturnType<typeof apiCall>` to extract API response types once and then use them safely everywhere. This method of dynamic type extraction was a lifesaver, especially since our APIs were constantly evolving—manual type updates were becoming a nightmare. In the end, managing legacy code with type safety became possible, and we almost stopped complaining about runtime errors when adding new features. TypeScript’s type system truly changed the future of the project, especially with conditional and mapped types in the mix.