from typing import List


def filter_sparse_fields(item: dict, sparse_fields: List[str]) -> dict:
    """
    Given a dictionary with the json:api representation of an item,
    drops any attributes or relationships that are not found in ``sparse_fields``
    and returns a new dictionary.

    For detailed information, check the json:api
    `spec <https://jsonapi.org/format/#fetching-sparse-fieldsets>`_.
    """
    new_item = item.copy()
    # filter `attributes`
    item_attributes = new_item.get('attributes')
    if item_attributes:
        new_attributes = item_attributes.copy()
        for attr_name in item_attributes:
            if attr_name not in sparse_fields:
                new_attributes.pop(attr_name, None)
        if new_attributes:
            new_item['attributes'] = new_attributes
        else:
            del new_item['attributes']
    # filter `relationships`
    item_relationships = new_item.get('relationships')
    if item_relationships:
        new_relationships = item_relationships.copy()
        for rel_name in item_relationships:
            if rel_name not in sparse_fields:
                new_relationships.pop(rel_name, None)
        if new_relationships:
            new_item['relationships'] = new_relationships
        else:
            del new_item['relationships']
    return new_item