```ts
/**
 * Parses a semantic version string and returns its components.
 *
 * @param version - The semantic version string to parse.
 * @returns An object containing the major, minor, patch, prerelease, and build parts of the version.
 */
export function parseSemver(version: string): { major: number; minor: number; patch: number; prerelease: string | null; build: string | null } {
    const regex = /^(\d+)\.(\d+)\.(\d+)(?:-(\w+))?(?:\+(\w+))?$/;
    const match = version.match(regex);

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

    const [_, major, minor, patch, prerelease, build] = match;

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