Writing Cleaner Code with TypeScript
TypeScript’s real strength isn’t just the red squiggles in your editor. Used well, it clarifies data models, makes function boundaries visible, and makes decision-making easier in large projects.
Use Types as Documentation
A well-written type is often understood faster than a long comment.
type PostStatus = "draft" | "published" | "archived";
type BlogPost = {
title: string;
slug: string;
status: PostStatus;
publishedAt?: Date;
};
This structure plainly shows what states a post can be in. When a new developer opens the file, they can read how the system works directly from the code.
Small Steps Instead of any
When you don’t know the type of a value, starting with unknown and narrowing is healthier than reaching for any.
function parseCount(value: unknown): number {
if (typeof value === "number") return value;
if (typeof value === "string") return Number(value);
return 0;
}
This style makes you write slightly more code, but it gives you a much better signal when an unexpected shape shows up later.
Conclusion
Think of TypeScript as part of project discipline. The clearer, smaller and closer to the real domain your types are, the more comfortably your codebase will scale.
Comments
Sign in with GitHub to post a comment.