```ts
export function parseSemver(version: string): { major: number; minor: number; patch: number; prerelease: string | null; build: string | null } {
  const re = /^v?(\d+)\.(\d+)\.(\d+)(?:-(\w+))?(?:\+(.+))?$/;
  const match = version.match(re);

  if (!match) {
    throw new Error(`Invalid semantic version format: ${version}`);
  }

  const major = parseInt(match[1], 10);
  const minor = parseInt(match[2], 10);
  const patch = parseInt(match[3], 10);

  return {
    major,
    minor,
    patch,
    prerelease: match[4] ? match[4].toLowerCase() : null,
    build: match[5] ? match[5] : null
  };
}
```