Typescript does a lot to automatically figure out types through regular Javascript code, but type predicates offer more direct control over types. A type predicate is a return type which takes the form of parameterName is Type. When the function is called with a variable, that variable will be narrowed to that specific type if it's compatible.
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}
As of TS 5.5, the return type is no longer necessary, making for less brittle predicates: https://www.totaltypescript.com/type-predicate-inference. The function itself does the narrowing, not the annotation.
This is a form of 'type guard'.