Skip to content

Instantly share code, notes, and snippets.

@kno3comma14
Created March 11, 2025 09:59
Show Gist options
  • Save kno3comma14/592cb7425050fd5fae094bc7e1406fd1 to your computer and use it in GitHub Desktop.
Save kno3comma14/592cb7425050fd5fae094bc7e1406fd1 to your computer and use it in GitHub Desktop.


how to handle empty datatypes at service layer in a typescript + nestjs web service

Handling empty data types in the service layer of a TypeScript + NestJS web service involves several strategies to ensure robustness and clarity in your application. Here's a structured approach to managing empty or null data types:

1. Use of Optional Parameters and Properties

In TypeScript, you can define optional parameters or properties by appending a ? after the parameter name. This automatically includes undefined in the type, allowing you to handle cases where data might be missing.

// Example of an optional property
class User {
  id: number;
  name?: string; // Optional property
}

// Example of an optional parameter
function greet(name?: string): void {
  if (name) {
    console.log(`Hello, ${name}!`);
  } else {
    console.log('Hello!');
  }
}

2. Null and Undefined Checks

Use strictNullChecks to ensure that null and undefined are not assignable to every type. This helps prevent errors by requiring explicit handling of these values.

// Enable strict null checks in your tsconfig.json
{
  "compilerOptions": {
    "strictNullChecks": true,
    // Other options...
  }
}

3. Custom Exceptions for Handling Errors

In NestJS, it's common to throw custom exceptions from the service layer. These exceptions can then be caught and handled by the controller layer, which is responsible for returning HTTP responses.

// Custom exception example
class EmptyDataError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'EmptyDataError';
  }
}

// Service layer example
@Injectable()
export class UserService {
  async getUser(id: number): Promise<User> {
    const user = await this.userRepository.findOne(id);
    if (!user) {
      throw new EmptyDataError(`User with ID ${id} not found.`);
    }
    return user;
  }
}

// Controller layer example
@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get(':id')
  async getUser(@Param('id') id: number): Promise<User> {
    try {
      return await this.userService.getUser(id);
    } catch (error) {
      if (error instanceof EmptyDataError) {
        throw new NotFoundException(error.message);
      }
      throw error;
    }
  }
}

4. Type Checking for Empty Objects

When dealing with objects, ensure they are not empty by using type guards or checks.

function isEmptyObject(obj: any): boolean {
  return Object.keys(obj).length === 0;
}

// Example usage
const data = {};
if (isEmptyObject(data)) {
  throw new EmptyDataError('Data object is empty.');
}

5. Using Record Type for Empty Objects in TypeScript

If you need to represent an empty object type in TypeScript, you can use the Record type with an empty type for keys.

type EmptyObject = Record<never, never>;

const emptyObj: EmptyObject = {};

This approach ensures that only an empty object can be assigned to the variable.

By implementing these strategies, you can effectively handle empty data types in your TypeScript + NestJS application, ensuring robust error handling and clear code structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment