Avoid using Object or {}
π΄ Object / {}
function printRowAsTable(data: Object) {
// Returns the data as a table representation, i.e.
// | Key | Value |
// |------|--------|
// | key1 | value1 |
// | key2 | value2 |
// | key3 | value3 |
// |------|--------|
}
β Example 1 - These are type-safe:
printRowAsTable({ name: "Alice", age: 30, isActive: true });
printRowAsTable({ "2010": 123, "2011": 456, "2012": 789 });
β οΈ Example 2 - These are also type-safe but logically incorrect:
printRowAsTable(new Date());
printRowAsTable([1, 2, 3, 4, 5]);
π’ Record
Invalid combinations like in Example 2 are now caught by the type system:
function printRowAsTable(data: Record<string, unknown>) {}