I'm trying to rewrite the signature of sum from core.ts in our compiler (see #16823), but I'm getting this error:
exportfunctionsum<TextendsRecord<K,number>,Kextendsstring>(array: T[],prop: K): number{letresult=0;for(constvofarray){result+=v[prop];// ~~~~~~~~~~~~~~~~~// Operator '+=' cannot be applied to types 'number' and 'T[K]'.}returnresult;}Note that I can't declare sum as
exportfunctionsum<Kextendsstring>(array: Record<K,number>[],prop: K): number
because then TypeScript would infer all of the properties in each element for K. So I do need another type argument T for the indirection.
For a simpler repro:
functionfoo<Textendsnumber>(x: T){return0+x;// ~~~~~// Operator '+' cannot be applied to types '0' and 'T'.}Related is #15645.
I'm trying to rewrite the signature of
sumfromcore.tsin our compiler (see #16823), but I'm getting this error:Note that I can't declare
sumasbecause then TypeScript would infer all of the properties in each element for
K. So I do need another type argumentTfor the indirection.For a simpler repro:
Related is #15645.