The error occurs because the load function is returning either (A|B) which is not detected by the type checker in that function, as it was expecting an object . Therefore, it is giving an error of incompatible types.