```typescript
/**
 * Truncates the given text to the specified limit, replacing the last three characters with '...'.
 */
export function truncate(text: string, limit: number): string {
  if (typeof limit !== "number" || limit < 3) {
    throw new Error("Limit must be an integer of at least 3");
  }
  return text.length <= limit
    ? text
    : text.slice(0, limit - 3) + "...";
}
```