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

Static typing vs type inference?

👁️ 8 views💬 3 replies❤️ 0 likes
SofiaWebDev🔥
SofiaWebDevUzman · Lv50
293 posts555 points
02 Tem 16:00
Do you guys prefer writing types manually or letting the compiler infer them? For example, do you write `const name: string = 'Sofia'` or trust TypeScript to figure it out on its own? Or do you mix both styles? Tell us why you choose your method.
3 Replies
KahveliKod
KahveliKodOrta · Lv35
485 posts3325 points
02 Tem 17:12
Dude, I really trust type inference more. Like, when I write `const name = 'Sofia'`, TypeScript automatically assigns the `string` type, making my code cleaner and more readable. I only manually intervene when dealing with complex functions or when I need to type API responses. For example, if a response from an API is of type `User`, I directly define `interface User { ... }` to avoid any chaos. But of course, everyone has their own preferences. For me, sticking to the DRY (Don't Repeat Yourself) principle is important. At least, I tell junior devs to "let type inference do its thing," and then they can fix it manually if needed 😅 For complex projects, I also manually type things to avoid any ambiguity.
RetiredAndLearning🌿
RetiredAndLearningAcemi · Lv18
267 posts545 points
02 Tem 17:58
Well, type inference has saved me from writing repetitive code. But hasn't it happened to you that in some cases the compiler gets it wrong and it's better to manually specify the type to avoid headaches?
AishaCodeX🌿
AishaCodeXAcemi · Lv15
51 posts53 points
02 Tem 18:56
In my small projects as a starting point, I prefer letting TS infer types wherever possible—like `const name = 'Sofia'`—for speed and not getting bogged down in writing types. But for shared functions or APIs, I manually define types for clarity and solid documentation. Both approaches work best for the situation!