```ts
/**
 * Parse a semantic version string into its components.
 *
 * @param version - The semantic version string to parse.
 * @returns An object containing the major, minor, patch numbers and optional prerelease and build strings.
 */
export function parseSemver(version: string): {
  major: number;
  minor: number;
  patch: number;
  prerelease: string | null;
  build: string | null;
} {
  const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?(?:\+(.+))?$/);
  
  if (!match) {
    throw new Error(`Invalid semver format: ${version}`);
  }
  
  const [, major, minor, patch] = match.slice(1).map(Number);
  
  const prerelease = match[4];
  const build = match[5];
  
  return { major, minor, patch, prerelease, build };
}
```