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

Changed return type of toArray to HTMLElement[] #11098

Merged
merged 3 commits into from
Sep 9, 2016

Conversation

LOZORD
Copy link
Contributor

@LOZORD LOZORD commented Sep 7, 2016

case 2. Improvement to existing type definition.

  • documentation or source code reference which provides context for the suggested changes. url http://api.jquery.com/html .
    • it has been reviewed by a DefinitelyTyped member.

NOTE Also changed the return type for get(void) and added a JSDoc @alias annotation.

Documentation See toArray() and get(void).

Due to legacy support, these functions are practically aliases.

Also changed the return type for `get(void)` and added a JSDoc `@alias` annotation.
@dt-bot
Copy link
Member

dt-bot commented Sep 7, 2016

jquery/jquery.d.ts

to authors (@borisyankov @choffmeister @Steve-Fenton @Diullei @tasoili @jasons-novaleaf @seanski @Guuz @ksummerlin @basarat @nwolverson @derekcicerone @AndrewGaspar @jameshfisher @seikichi @benjaminjackman @s093294 @JoshStrobl @johnnyreilly @DickvdBrink). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@LOZORD
Copy link
Contributor Author

LOZORD commented Sep 7, 2016

Failure: https://travis-ci.org/DefinitelyTyped/DefinitelyTyped/builds/158300393#L1213

Should I be using HTMLElement? It seems that the vast majority of the definitions use Element instead of HTMLElement.

@thorn0
Copy link
Contributor

thorn0 commented Sep 7, 2016

Think of $(window).toArray()

@LOZORD
Copy link
Contributor Author

LOZORD commented Sep 8, 2016

@thorn0, that's a fair point, but I feel like that is kind of a "garbage-in-garbage-out scenario". When would you need to actually use $(window).toArray()? Maybe definitions need to be made more strict?

@johnnyreilly
Copy link
Member

Could you address the failing tests please? If Element is what's generally used now I'd stick with that for consistency. If there's a need then moving from Element to HTMLElement could be handled in a subsequent PR which addresses all instances at once. But I'm not convinced it's necessary really.

@sheetalkamat sheetalkamat added the Revision needed This PR needs code changes before it can be merged. label Sep 8, 2016
@sheetalkamat
Copy link
Contributor

Thank you for your contribution.
Can you please fix the failing Travis CI tests and update the PR!

Now the __only__ return type is `HTMLElement`, which should solve the Travis build/test failure.
@LOZORD LOZORD changed the title Changed return type of toArray to Element[] Changed return type of toArray to HTMLElement[] Sep 8, 2016
@LOZORD
Copy link
Contributor Author

LOZORD commented Sep 8, 2016

Ok, tests pass @johnnyreilly @sheetalkamat 🎉

I switched Element for HTMLElement to get the tests to pass. It's up to you whether you want the "generality" (and majority) of Element or not.

@johnnyreilly
Copy link
Member

Thanks!

@johnnyreilly johnnyreilly merged commit 15d88ee into DefinitelyTyped:master Sep 9, 2016
@dedocibula
Copy link

Hi, having a problem with this when using in conjunction with mapping function.

For instance:
$('a').map((_, a) => a.href).get()
or
$('a').map((_, a) => a.href).toArray()

Expected output should be array of strings but Typescript compiler won't allow cast from HTMLElement[] to string[]. any[] used to work fine with additional casting. Please, reconsider.

@LOZORD
Copy link
Contributor Author

LOZORD commented Feb 27, 2017 via email

@dedocibula
Copy link

Yeah, functionality-wise these notations are definitely equivalent. I just wanted to reiterate on the fact that this change was motivated by JQuery doc for get(void), yet JQuery doc for map suggests that the above should work, even highlighting it as an example.

@LOZORD
Copy link
Contributor Author

LOZORD commented Feb 28, 2017 via email

@dedocibula
Copy link

dedocibula commented Feb 28, 2017

Not sure if I understood your parametrized idea but I think there are essentially 2 possible approaches.

  1. Making toArray() and both get() methods generic

    • this will essentially require to always specify return type, obviously not super convenient - not specifying means {} a.k.a. Object which is typically wrong
  2. Having generic JQuery interface

    • this is obviously way more work as it essentially requires changes throughout the entire jquery.d.ts. It will be necessary to analyze all possible return values for JQueryStatic () function declarations but in the end you end up with map() method equivalent to that of arrays in lib.d.ts and both toArray() and get() get solved automatically.

@LOZORD
Copy link
Contributor Author

LOZORD commented Feb 28, 2017

We might be able to be lazier (approach 1) and take advantage of default type parameters. From my understanding, JQuery acts as a wrapper around HTMLElements (hence my belief in toArray(): HTMLElement[]), but as you pointed out, there are counterexamples. So by default, JQuery(Static) would be parametrized by HTMLElement[], but calling map<string> for example, would change toArray() to return string[].

@dedocibula
Copy link

Yeah, was about to write about the default types. For backwards compatibility I would maybe suggest reverting back to any[] and once TS 2.3 is released switch to toArray<T = any>() and get<T = any>(), or toArray<T = HTMLElement>() and get<T = HTMLElement>().

@LOZORD
Copy link
Contributor Author

LOZORD commented Apr 27, 2017

TS 2.3 is out!

I can make the updates soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants