```typescript
/** Returns the truncated text, with ellipsis if necessary; throws Error if limit is invalid */
export function truncate(text: string, limit: number): string {
  /**
   * @throws {Error} If limit is not a positive integer or less than 3.
   */
  if (typeof limit !== 'number' || limit <= 2) {
    throw new Error('limit must be a positive integer of at least 3');
  }
  return text.length <= limit ? text : text.slice(0, limit - 1) + '...';
}
```