Member-only story

How to Write a Custom ESLint Rule for Interface Properties Alignment

Learn_With_Awais
3 min readJan 2, 2025

--

In large TypeScript projects, ensuring code consistency is vital for maintainability and readability. One common requirement is the alignment of interface properties, especially when there are multiple properties with type annotations. Misaligned properties can make code harder to read and review. In this article, we’ll create a custom ESLint rule to enforce alignment of interface properties in TypeScript interfaces

What This Rule Will Do

Our custom rule will ensure that:

Properties in an interface are aligned so that their type annotations (:) are in the same column.

Developers can specify additional padding between property names and the type annotation.

Configuration

We’ll call this rule interface-properties-alignment. It will accept a single option, padding, to define the extra spaces between the longest property name and the colon (:).

"@nx/workspace-interface-properties-alignment": [
"warn",
{ "padding": 2 }
]

Implementation

Here’s the implementation of the ESLint rule:

1. Define Rule Metadata

--

--

Learn_With_Awais
Learn_With_Awais

Written by Learn_With_Awais

“Angular Developer | Tech Enthusiast | Sharing insights on modern web development, AI tools, and productivity hacks. Let’s build smarter, together! 🚀”

No responses yet