A general theory for target reproduction numbers with applications to ecology and epidemiology