```ts
/**
 * Splits one line of CSV into its fields on commas. A field wrapped in double quotes may contain commas,
 * and a doubled quote inside such a field means one literal quote character; the surrounding quotes are not
 * part of the value. Fields that are not quoted are taken literally, including any surrounding spaces.
 * An empty line is one empty field. Throws an Error for a quoted field that is never closed.
 *
 * @param line - The CSV line to parse.
 * @returns An array of fields.
 */
export function parseCsvLine(line: string): string[] {
  const result: string[] = [];
  let field = '';
  let inQuotes = false;
  let escapeNext = false;

  for (let i = 0; i < line.length; i++) {
    const char = line[i];

    if (escapeNext) {
      field += char;
      escapeNext = false;
      continue;
    }

    if (char === '"') {
      if (inQuotes && i + 1 < line.length && line[i + 1] === '"') {
        field += '"';
        i++; // Skip the next quote
        continue;
      }
      inQuotes = !inQuotes;
    } else if (char === ',' && !inQuotes) {
      result.push(field.trim());
      field = '';
    } else {
      field += char;
    }

    if (char === '\\') escapeNext = true;
  }

  if (inQuotes) throw new Error('Unquoted quote found');

  result.push(field.trim());
  return result;
}
```