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

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

  const [, major, minor, patch] = match.slice(1);
  
  return { 
    major: parseInt(major, 10),
    minor: parseInt(minor, 10),
    patch: parseInt(patch, 10),
    prerelease: match[4],
    build: match[5]
  };
}
```