10 TypeScript Tips and Tricks for Advanced Developers

Skylar Johnson
5 min readAug 10, 2023

--

For a beginner, TypeScript can be overwhelming at times. However, with the right tips and techniques, you can harness the power of TypeScript to write safer, more reliable code. In this article, I share 10 TypeScript Tips to help you improve your development workflow and improve your understanding of the language.

Read Also: How do I save ChatGPT Conversations for Later?

Enable Strict Mode

One of the best features of TypeScript is its strict type-checking system. By enabling strict mode, TypeScript performs more thorough type checking and provides better error messages. To enable strict mode, add the "strict": true option to your tsconfig.json file:

Example

{
"compilerOptions": {
"strict": true
}
}

Enabling strict mode from the beginning of your project will help you catch potential errors early on and ensure better code quality.

Use Explicit Types

TypeScript is about types, so it’s important to be explicit when defining types. Avoid relying on type inference and be explicit about variable types, function parameters, and return values. This improves the readability of your code and makes it easier for other developers to understand your code. For example:

Example

function addNumbers(a: number, b: number): number {
return a + b;
}

Take Advantage of Interfaces

Interfaces in TypeScript allow you to define the shape of objects and specify the types of their properties. They are a powerful tool for creating reusable and maintainable code. Instead of using inline type annotations, consider defining interfaces for complex data structures. For example:

Example

interface User {
name: string;
age: number;
email: string;
}
function sendEmail(user: User) {
// ...
}

Using interfaces not only improves code clarity but also allows type-checking of object properties.

Utilize Union Types and Type Guards

Union types allow you to define a variable that can have multiple types. This is useful when dealing with situations where a variable may have different possible values. Type guards like typeof and instanceof, help you narrow down the type in a conditional block. Here's an example:

By using union types and type protection, you can write more flexible and robust code that handles different scenarios.

Example

type Shape = 'circle' | 'square' | 'triangle';
function getArea(shape: Shape, size: number): number {
if (shape === 'circle') {
return Math.PI * size * size;
} else if (shape === 'square') {
return size * size;
} else if (shape === 'triangle') {
return (Math.sqrt(3) / 4) * size * size;
}
}
const area = getArea('circle', 5);

By using union types and type protection, you can write more flexible and robust code that handles different scenarios.

Destructure Objects and Arrays

Destructuring is a handy feature of TypeScript that lets you extract values ​​from objects and arrays. You can make your code more concise and readable. Instead of directly accessing object properties or array elements, you can break them down into separate variables. Here is an example:

Example

interface Person {
name: string;
age: number;
address: string;
}
function greet(person: Person) {
const { name, age } = person;
console.log(`Hello, ${name}! You are ${age} years old.`);
}
const user = { name: 'Alice', age: 25, address: '123 Main St' };
greet(user);

Destructuring not only simplifies your code but also reduces the need to repeatedly access objects or arrays.

Use Generics for Reusable Code

Generics allow you to create reusable code components that work with different types. They provide flexibility and type safety by allowing you to parameterize types in functions, classes, and interfaces. By using generics, you can write functions or classes that can work with a variety of data types while maintaining type safety. Here is an example:

Example

function identity<T>(value: T): T {
return value;
}
const result = identity<number>(42);

In this example, the identity function takes a generic type parameter T to indicate that it can accept and return any type. When calling the function, you can either explicitly specify the type (e.g. "identity<number>(42))") or let TypeScript infer the type based on the argument.

Use Type Assertion

Type assertion is a way to tell the TypeScript compiler the specific type of a value when it contains more information than the type checker. It allows you to override the derived type and treat the value as another type. Use type assertion with caution and only when you are sure of type compatibility. Here is an example:

Example

let value: any = 'Hello, TypeScript!';
let length: number = (value as string).length;

In this case, the value is explicitly asserted as a string using the keyword. This provides access to the length property specific to strings.

Take Advantage of Type Inference

TypeScript has powerful type inference capabilities, which means it can automatically infer the type of a variable based on its value. You can use type inference to reduce the need for explicit type annotations and make your code more concise and readable. Here is an example:

Example

let message = 'Hello, TypeScript!';
// TypeScript infers the type of 'message' as string
let numbers = [1, 2, 3, 4, 5];
// TypeScript infers the type of 'numbers' as number[]

In these examples, TypeScript infers variable types based on assigned values. Note, however, that explicit type annotations can still be useful for improving code clarity, especially in complex scenarios.

Use Optional and Default Function Parameters

With TypeScript, you can define optional function parameters using a ? add. after the parameter name. This allows you to specify default values ​​when calling a function, or to make certain parameters optional. Here is an example:

Example

function greet(name: string, greeting?: string) {
if (greeting) {
console.log(`${greeting}, ${name}!`);
} else {
console.log(`Hello, ${name}!`);
}
}
greet('Alice'); // Hello, Alice!
greet('Bob', 'Hi'); // Hi, Bob!

In this case, the greeting parameter is optional. If not specified, the function uses a default greeting.

Use Intersection Types for Flexible Type Composition

Intersection types allow you to combine multiple types into a single type, creating a new type that has all the properties and methods of each type. This provides flexibility when typing and can be particularly useful when dealing with complex object structures. Here is an example:

Example

type Greeting = {
greet(): void;
};
type Farewell = {
sayGoodbye(): void;
};
type GreetingAndFarewell = Greeting & Farewell;class Person implements GreetingAndFarewell {
greet() {
console.log('Hello!');
}
sayGoodbye() {
console.log('Goodbye!');
}
}
const person = new Person();
person.greet(); // Hello!
person.sayGoodbye(); // Goodbye!

In this example, we are defining Hello and Goodbye types, which represent objects that have specific methods. Using the intersection type operator (&), we create a new GreetingAndFarewell type that combines the two types. The Person class then implements the GreetingAndFarewell type, which requires you to provide implementations for the greeting and farewell methods.

Using intersection types, you can type flexibly and create reusable layouts to represent complex structures and behaviors in your TypeScript code.

Conclusion

TypeScript is a powerful programming language that provides static typing and advanced features for JavaScript development. These tips will help you write cleaner, more maintainable code and increase your productivity.

Take advantage of TypeScript’s robust type system and take advantage of the support load of tools to build apps from scalable code Remember to keep up with the latest TypeScript advancements and keep learning how to stay ahead of the ever-changing world of web development.

Get started apply these tips today and use the full potential of TypeScript in your projects. Happy coding!

--

--

Skylar Johnson
Skylar Johnson

Written by Skylar Johnson

I'm a Web developer who is always looking to learn more and eat too much chocolate. https://www.thetravelocity.com

Responses (6)