```typescript
export function parseSemver(version: string): { major: number; minor: number; patch: number; prerelease: string | null; build: string | null } {
  const regex =
    /^(0|[1-9][0-9]*)\.([0-9]+)\.([0-9]+)(-(pre|beta|rc|alpha|test)[0-9a-zA-Z-]*|\+build[0-9a-zA-Z-]+)?$/;
  const match = version.match(regex);

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

  const [, major, minor, patch] = match.slice(1, 4);
  const prerelease = match[4];
  const build = match[5];

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