Skip to content

Get the correct type of elements with querySelector #45787

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

Closed
5 tasks done
zuhairtaha opened this issue Sep 8, 2021 · 1 comment
Closed
5 tasks done

Get the correct type of elements with querySelector #45787

zuhairtaha opened this issue Sep 8, 2021 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@zuhairtaha
Copy link

Suggestion

πŸ” Search Terms

document query selector elements type

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

When we use document.querySelector and querySelectorAll I suggest getting the correct element type instead of Element if it starts with a valid HTML tag

πŸ“ƒ Motivating Example

const ul = document.querySelector('ul.list');
const input = document.querySelector('.container input[type=text]');

The inferred type of ul should be HTMLUListElement instead of Element
and for input it should be HTMLInputElement not Element

πŸ’» Use Cases

With this feature we will not need to write something like this

const input = document.querySelector('.container input[type=text]') as HTMLInputElement;

Note
In some cases, it is obviously an item type, but in other cases cannot be discovered like querySelector('.menu')

@andrewbranch
Copy link
Member

Duplicate of #29037

@andrewbranch andrewbranch marked this as a duplicate of #29037 Sep 8, 2021
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants