```ts
/**
 * Truncates the given text to `limit` characters, replacing the last three characters with '...'.
 * The total length of the output will be at least `limit`.
 *
 * @param text - The input string to truncate.
 * @param limit - The maximum number of characters to keep in the truncated string. Must be an integer of at least 3.
 */
export function truncate(text: string, limit: number): string {
  if (!Number.isInteger(limit) || limit < 3) {
    throw new Error("limit must be an integer of at least 3");
  }

  return text.length <= limit ? text : text.slice(0, limit - 4) + "...";
}
```