Course contentsShow
← TypeScript
Lesson 298 of 1,7967. ObjectsFree lesson

Trailing Separators in Object Types

Allowing optional trailing commas or semicolons for cleaner diffs and easier property addition.

Trailing Separators in Object Types

What you'll learn: How optional trailing commas or semicolons in object type definitions make your code easier to maintain.

Why Trailing Separators Matter

In TypeScript object types, you can optionally include a comma or semicolon after the last property. This might seem trivial, but it makes a real difference when working with version control and adding properties over time.

Think of it like a grocery list where you always end with a comma—even on the last item. That way, when you add another item tomorrow, you only change one line instead of two (adding the new item and editing the previous line to add its comma).

The Syntax

TypeScript allows both of these styles:

type User = {
  name: string;
  age: number;  // ← trailing semicolon allowed
};

type Product = {
  id: number,
  price: number,  // ← trailing comma allowed
}

Both are completely valid. You can even mix separators within the same type, though consistency is cleaner.

The Practical Benefit

Without trailing separators, adding a property requires touching two lines in your diff:

type User = {
  name: string;
  age: number     // ← must add separator here
  email: string;  // ← new line
};

With trailing separators, you only touch one:

type User = {
  name: string;
  age: number;    // ← already has separator
  email: string;  // ← clean one-line addition
};

This creates cleaner git diffs and reduces merge conflicts when multiple developers are adding properties.

Key Takeaway: Always include a trailing comma or semicolon after your last object type property—it's a small habit that makes property additions cleaner and version control friendlier.