```ts
/**
 * Parses a semantic version string into its components.
 * @param {string} version - The version string to parse.
 * @returns {{
 *   major: number,
 *   minor: number,
 *   patch: number,
 *   prerelease: string | null,
 *   build: string | null
 * }}
 * @throws {Error} If the input does not match the expected format.
 */
export function parseSemver(version: string): { 
  major: number; 
  minor: number; 
  patch: number; 
  prerelease: string | null; 
  build: string | null 
} {
  const regex = /^(\d+)\.(\d+)\.(\d+)(?:-(.*?))?(?:\+(.*))?$/;
  const match = version.match(regex);
  
  if (!match) {
    throw new Error('Invalid semver format');
  }

  return {
    major: parseInt(match[1], 10),
    minor: parseInt(match[2], 10),
    patch: parseInt(match[3], 10),
    prerelease: match[4] || null,
    build: match[5] || null
  };
}
```