```typescript
/**
 * Truncates the given text to the specified limit, replacing any excess with an ellipsis.
 * The returned string is at most 'limit' characters long. If 'text' is already shorter than or equal to 'limit', it returns unchanged.
 */
export function truncate(text: string, limit: number): string {
  if (typeof limit !== "number" || limit < 3) {
    throw new Error("Limit must be an integer at least 3");
  }
  return text.length > limit ? text.slice(0, limit - 2) + "..." : text;
}
```