Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

new rule: enforce one of the two Underscore function call forms #44

Closed
danielmanesku opened this issue Oct 11, 2015 · 5 comments
Closed
Assignees
Milestone

Comments

@danielmanesku
Copy link
Contributor

What do you think of creating a rule for enforcing one of the two forms of Underscore functions? Should we create a rule for this?

_.min(list, function() {...});
_(list).min(function() {...});

Personally, I prefer the second form.

@HamletDRC
Copy link
Member

Sounds good to me. I prefer the 2nd one too.

@HamletDRC HamletDRC changed the title Rule for enforcing one of the two Underscore function call forms new rule: enforce one of the two Underscore function call forms Oct 12, 2015
@loicraux loicraux self-assigned this Oct 25, 2015
@loicraux
Copy link
Contributor

Does this apply to all Underscore methods ? Or should it apply to only a subset of the methods (like the ones for collections (http://underscorejs.org/#collections) ?

@danielmanesku
Copy link
Contributor Author

I would say collections and arrays (at least)

@loicraux
Copy link
Contributor

Not sure if we can enable this rule... :

I just discovered that this code does not build :

function foo(dic: _.Dictionary<number>) : void {
    _(dic).forEach((val: number, key: string): void => {});
}

Error is :

error TS2345: Argument of type '(val: number, key: string) => void' is not assignable to
parameter of type 'ObjectIterator<Dictionary<number>, void>'.
  Types of parameters 'val' and 'element' are incompatible.
    Type 'number' is not assignable to type 'Dictionary<number>'.
      Index signature is missing in type 'Number'.

Whereas this one does build :

function foo(dic: _.Dictionary<number>) : void {
    _.forEach(dic, (val: number, key: string): void => {});
}

I don't know if this is an error in underscore.d.ts ... I have to check.

@danielmanesku
Copy link
Contributor Author

Might be a bug in our d.ts. file.

This works:

_.forEach([1, 2, 3], function(num) { console.log(num); })
_([5, 6, 7]).forEach(function(num) { console.log(num); })

Test it here: https://jsfiddle.net/kms3y8b8/. Enable developer tools to see the output and click on Run.

@HamletDRC HamletDRC assigned HamletDRC and unassigned loicraux Aug 1, 2016
@HamletDRC HamletDRC added this to the 2.0.10 milestone Aug 1, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants