```ts
/**
 * A badge written out from its fields.
 */
export function writeTagMarks(label: string, fields: string[][]): string {
  if (typeof label !== 'string' || !/^[a-z]+[a-z\d]*$/.test(label)) {
    throw new Error('Invalid label');
  }
  if (!Array.isArray(fields) || fields.some(field => !Array.isArray(field) || field.length !== 2)) {
    throw new Error('Invalid fields');
  }

  const seenKeys = new Set<string>();
  for (const [key] of fields) {
    if (seenKeys.has(key)) {
      throw new Error('Duplicate key');
    }
    seenKeys.add(key);
  }

  const parts = fields.map(([key, text]) => {
    if (typeof key !== 'string' || !/^[a-z]+[a-z\d]*$/.test(key)) {
      throw new Error('Invalid key');
    }
    if (typeof text !== 'string') {
      throw new Error('Invalid text');
    }

    if (/^[-.\w]+$/.test(text)) {
      return key + '=' + text;
    } else {
      const quote = text.includes('"') ? "'" : '"';
      const escapedText = text.replace(/\\/g, '\\\\').replace(new RegExp(quote, 'g'), '\\' + quote);
      return key + '=' + quote + escapedText + quote;
    }
  });

  return "<" + label + " " + parts.join(" ") + ">";
}
```