Semicolons vs Commas in Object Types
You'll learn the two valid ways to separate properties in TypeScript object type literals and when to use each.
The Comma and Semicolon Choice
When defining object type literals in TypeScript, you can separate properties using either commas (,) or semicolons (;). Both are completely valid, and TypeScript treats them identically:
// Using commas
type Person = {
name: string,
age: number,
active: boolean
};
// Using semicolons
type Employee = {
name: string;
age: number;
active: boolean;
};
// Both work exactly the same!
const user: Person = { name: "Alice", age: 30, active: true };
const worker: Employee = { name: "Bob", age: 25, active: false };
Think of it like choosing between a period or semicolon in writing — both end a sentence, just with slightly different style.
The Community Convention
While both are valid, semicolons are more common in the TypeScript community for type definitions. This mirrors how interfaces are written (where semicolons are standard) and creates visual consistency:
interface User {
id: number;
email: string;
}
type Settings = {
theme: string;
notifications: boolean;
};
However, commas match the syntax of actual JavaScript object literals, so some developers prefer them for familiarity.
Mixing (Not Recommended)
You can technically mix them, but don't:
// Valid but confusing — pick one style!
type Mixed = {
first: string,
second: number;
third: boolean
};
Key Takeaway: Both commas and semicolons work for separating properties in object types, but semicolons are the community standard and match interface syntax, while commas mirror JavaScript object literals — choose one style and stay consistent.