Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type argument for type parameter 'TResult' cannot be inferred #35

Closed
HomenSimpsor opened this issue Sep 29, 2015 · 2 comments
Closed

Type argument for type parameter 'TResult' cannot be inferred #35

HomenSimpsor opened this issue Sep 29, 2015 · 2 comments

Comments

@HomenSimpsor
Copy link

Hey @donnut, thanks for doing these definitions. I have a question that might have a simple answer. I can't figure out how to make a legal use of R.reduce:

/// <reference path="typings/ramda/ramda.d.ts"/>

import R = require('ramda');

var add = (item: number, sum: number): number => sum + item

var avg = (list: Array<number>): any => {
  var sum: number = R.reduce(add, list);
  return sum / list.length;
}

error TS2453: The type argument for type parameter 'TResult' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
  Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'number[]'.
@donnut
Copy link
Collaborator

donnut commented Sep 29, 2015

Hi @Wideshanks

reduce expects another argument, the initial value of the accumulator sum. If you specify:

var sum: number = R.reduce(add, 0, list);

it will probably work.

BTW, Typescript can infere the return types of add, any and the call to reduce, so you could simplify the code to:

    var add = (item: number, sum: number) => sum + item

    var avg = (list: Array<number>) => {
        var sum = R.reduce(add, 0, list);
        return sum / list.length;
    }

@HomenSimpsor
Copy link
Author

Ah, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants