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.
Static typing vs type inference?
👁️ 8 views💬 3 replies❤️ 0 likes
3 Replies
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.
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?
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!