I found the answer in the official haxe documentation. Here it is:
public static function dynamicCast<T : Foo>(target : Foo, cls : Class<T>) : T
{
if(Std.is(target, cls))
{
var ret : T = cast target;
return ret;
}
return null;
}