```ts
export function fillTemplate(template: string, values: Record<string, string>): string {
  if (typeof template !== 'string') {
    throw new Error('Template must be a string');
  }

  let result = '';
  let i = 0;

  while (i < template.length) {
    if (template[i] === '{') {
      const end = template.indexOf('}', i);
      if (end === -1) {
        throw new Error('Unterminated placeholder');
      }
      const name = template.slice(i + 1, end);
      if (!name || /[^a-zA-Z0-9_]/.test(name)) {
        throw new Error('Invalid placeholder name');
      }
      result += values.hasOwnProperty(name) ? values[name] : `{${name}}`;
      i = end + 1;
    } else {
      result += template[i];
      i++;
    }
  }

  return result;
}
```