Detecting if other GameObject collided with has a certain component

How do you detect if the other gameobject you collide with has a certain component (before attempting to GetComponent)?

If the “certain component” is a rigidbody, you can test ControllerColliderHit.rigidbody (or Collision.rigidbody, if your object isn’t a CharacterController):

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.rigidbody){
    // object hit has a rigidbody
  }
}

Other components can only be checked using GetComponent:

function OnControllerColliderHit(hit: ControllerColliderHit){
  var comp: CertainComponent = hit.transform.GetComponent(CertainComponent);
  if (comp){
    // object hit has the component
  }
}