How to find a specific post_type using its “rewrite slug”?

There are two ways I’ve used for doing this…each with issues:

This one this only works if you’ve set the rewrite slug for your CPT. if you don’t set one it could lead to an error:

$post_type = get_queried_object();
echo $post_type->rewrite['slug'];

This one will not work on archive pages, because the page type will be “page”:

$post_type = get_post_type();
if ( $post_type )
{
    $post_type_data = get_post_type_object( $post_type );
    $post_type_slug = $post_type_data->rewrite['slug'];
    echo $post_type_slug;
}