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

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

  const [_, major, minor, patch] = match.slice(1);
  const prerelease = match[3] || null;
  const build = match[4] || null;

  return { major: parseInt(major, 10), minor: parseInt(minor, 10), patch: parseInt(patch, 10), prerelease, build };
}
```