WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Guru · question 97 of 100

What are the best practices to handle ’any’ types in a large TypeScript project?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

Handling ‘any‘ types in a large TypeScript project can be tricky, but the following best practices help maintain a strict, type-safe environment that leads to fewer bugs and improved maintainability.

1. **Minimize usage of ‘any‘**: Avoid using the ‘any‘ type as much as possible. It disables type checking and could cause unexpected runtime issues. Instead, opt for strict types or use union types where necessary.

2. **Use ‘unknown‘ when applicable**: If you have a value that could be of any type and you want to enforce a type check before using it, use the ‘unknown‘ type instead of ‘any‘. An ‘unknown‘ type forces developers to perform type checking or type narrowing before using the value.

Example:

   function handleValue(value: unknown): number {
     if (typeof value === "number") {
       return value;
     } else {
       throw new Error("Value must be a number.");
     }
   }

3. **Type assertions**: If you are certain about a value’s type, you can use type assertions to convert between types. This should only be used with caution after proper checks are in place.

Example:

   function handleValue(value: unknown): number {
     if (typeof value === "number") {
       return value as number;
     } else {
       throw new Error("Value must be a number.");
     }
   }

4. **Leverage ‘type guards‘**: Use custom type guard functions to safely handle different types.

Example:

   type Animal = { type: "dog"; age: number } | { type: "cat"; age: number };

   function isDog(animal: Animal): animal is { type: "dog"; age: number } {
     return animal.type === "dog";
   }

   function handleAnimal(animal: Animal) {
     if (isDog(animal)) {
       console.log("Handling a dog of age", animal.age);
     } else {
       console.log("Handling a cat of age", animal.age);
     }
   }

5. **Use third-party type-safe libraries**: When using third-party libraries that don’t have type definitions, prefer using libraries that have type-safe alternatives. For example, for Lodash, use ‘lodash-ts‘ or consult ‘DefinitelyTyped‘ for type definitions.

6. **Employ strict mode**: In your ‘tsconfig.json‘ file, enable the ‘strict‘ flag or enable ‘strictNullChecks‘, ‘strictFunctionTypes‘, and other related flags individually. This will make TypeScript more strict and require your code to be more type-safe.

Example ‘tsconfig.json‘:

   {
     "compilerOptions": {
       "strict": true,
       /* or enable individual flags */
       "strictNullChecks": true,
       "strictFunctionTypes": true
     }
   }

In summary, try to minimize the use of ‘any‘ types in your TypeScript projects. Use ‘unknown‘ when applicable, apply type assertions responsibly, leverage type guards, prefer type-safe third-party libraries, and enable strict mode to help maintain a type-safe and more maintainable code base.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics