TypeScript Tips for Better Code

Destiny Ezenwata
2 min read
TypeScript
JavaScript
Programming

TypeScript Tips for Better Code

TypeScript has become the standard for building robust JavaScript applications. Here are some tips to level up your TypeScript skills.

Use Type Inference

Let TypeScript infer types when possible:

// Good - type is inferred
const numbers = [1, 2, 3]

// Unnecessary - type annotation
const numbers: number[] = [1, 2, 3]

Utility Types Are Your Friend

TypeScript provides built-in utility types:

// Pick specific properties
type UserPreview = Pick<User, "id" | "name">

// Make all properties optional
type PartialUser = Partial<User>

// Make all properties required
type RequiredUser = Required<User>

Use Discriminated Unions

Create type-safe state machines with discriminated unions:

type LoadingState =
	| { status: "idle" }
	| { status: "loading" }
	| { status: "success"; data: string }
	| { status: "error"; error: Error }

Generic Constraints

Make your generics more specific with constraints:

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
	return obj[key]
}

Never Use 'any'

Avoid any at all costs. Use unknown if you truly don't know the type:

// Bad
function processData(data: any) {}

// Good
function processData(data: unknown) {
	if (typeof data === "string") {
		// TypeScript knows data is a string here
	}
}

Conclusion

These TypeScript tips will help you write more maintainable and type-safe code. Keep learning and exploring TypeScript's powerful type system!