type SumFunc = { param source returns ( source ) : Promise < number > ; param source param selector returns < TSource > ( source , selector ) : Promise < number > ; } ; export const sum = < TSource > ( source , selector ? ) : Promise < number > => { if ( selector ) { return sum2 ( source as AsyncIterable < TSource > , selector ) ; } else { return sum1 ( source as AsyncIterable < number > ) ; } } ; const sum1 = async ( source ) : Promise < number > => { let total = 0 ; for await ( const value of source ) { total += value ; } return total ; } ; const sum2 = async < TSource > ( source , selector ) : Promise < number > => { let total = 0 ; for await ( const value of source ) { total += selector ( value ) ; } return total ; } ;