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

Use native querySelector types instead of copying them #31

Closed
wants to merge 2 commits into from

Conversation

fregante
Copy link
Collaborator

@fregante fregante commented Dec 28, 2020

I'm looking for a way to use querySelector’s own types so that:

  • they don't need to be copied to our definitions
  • enhanced 4.1 querySelector types would be enabled here as well (via typed-query-selector or natively)

Unfortunately I'm stuck on "Type QuerySelector is not generic":

Type * is not a generic

Note: We can't use Parameters<fn> and such because it doesn't support overloads.

Copy link

@g-plane g-plane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.d.ts Outdated Show resolved Hide resolved
index.d.ts Show resolved Hide resolved
index.d.ts Show resolved Hide resolved
Co-authored-by: Pig Fang <g-plane@hotmail.com>
@fregante
Copy link
Collaborator Author

fregante commented Dec 29, 2020

With the help of this SO answer, I'm starting to understand why this isn't working (but I'm not any closer to a solution).

In short, ParseSelector<string> is like a "TS type function" that accepts a "parameter" and it resolves into another type. querySelector<string>() instead is an actual JS function that's resolved when it's actually called in JavaScript. The closest way to extract its resulting type is via ReturnType<querySelector>, which ignores the overloads and the generics.


I think this feature request is exactly what I need to do, but it hasn't been accepted. Its alternative solution includes cascades of extends/infer, as expected. I don't think that's worth over just including typed-query-selector


The next ideal solution would be to set elementReady: typeof querySelector and then add the second parameter. Unfortunately this is likely even less doable.

@fregante fregante closed this Dec 29, 2020
@fregante fregante deleted the use-native-querySelector-types branch December 29, 2020 01:33
@fregante
Copy link
Collaborator Author

Maaybe this? microsoft/TypeScript#33185 (comment)

@sindresorhus
Copy link
Owner

@fregante Can you move this to an issue? In case someone comes along and knows a good way to solve this.

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

Successfully merging this pull request may close these issues.

None yet

3 participants