You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to add numeric types that are not simply 'number' #17069
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'.}
changed the title [-]Unable to add types that are not simply 'number'[/-][+]Unable to add numeric types that are not simply 'number'[/+]on Jul 10, 2017
Activity
[-]Unable to add types constrained to 'number'[/-][+]Unable to add types that are not simply 'number'[/+][-]Unable to add types that are not simply 'number'[/-][+]Unable to add numeric types that are not simply 'number'[/+]Fix #17069 and #15371
sandersn commentedon Aug 17, 2017
Fix is up at #17870
sandersn commentedon Aug 17, 2017
Note that #17404 fixed the simpler repro; #17870 fixes the original issue with a type parameter indexed by another type parameter.
Fix #17069 and #15371
sandersn commentedon Aug 18, 2017
The previous PR got merged by mistake and is now reverted; #17912 is the PR with the fix now.