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

'HTMLElement' is not defined when used in markup event handler #6643

Closed
techniq opened this issue Aug 13, 2021 · 8 comments · Fixed by #6665
Closed

'HTMLElement' is not defined when used in markup event handler #6643

techniq opened this issue Aug 13, 2021 · 8 comments · Fixed by #6665
Labels
bug compiler Changes relating to the compiler good first issue A small, encapsulated issue, ideal for a new contributor to Svelte to tackle.

Comments

@techniq
Copy link

techniq commented Aug 13, 2021

Describe the bug

If HTMLElement is used in an event handler, for example:

<button on:click={e => { if (e.target instanceof HTMLElement) { } }}>Click me</button>

It produces the following unexpected warning:

'HTMLElement' is not defined. Consider adding a <script> block with 'export let HTMLElement' to declare a prop (5:49)

If you use Element in the handler, no warning is produced:

<button on:click={e => { if (e.target instanceof Element) { } }}>Click me</button>

or if you pull the handler into a function within <script>

<script>
  function onClick() {
    if (e.target instanceof HTMLElement) {

    }
  }
</script>

<button on:click={onClick}>Click me</button>

Reproduction

REPL

Logs

No response

System Info

See REPL

Severity

annoyance

@Conduitry Conduitry added bug good first issue A small, encapsulated issue, ideal for a new contributor to Svelte to tackle. compiler Changes relating to the compiler labels Aug 13, 2021
@himanshu007-creator
Copy link

Hi, I would like to work on this issue, can someone guide me further on what needs to be done?
Thanks

@dummdidumm
Copy link
Member

You need to enhance the list of known globals, see this PR for inspiration: #5601

@techniq
Copy link
Author

techniq commented Aug 16, 2021

Might be good to go ahead and add SVGElement when HTMLElement is added.

I'm guessing we're not wanting this list to be exhaustive as otherwise this list would be quite large, even for just HTML/SVG elements...

CleanShot 2021-08-16 at 08 23 41@2x

FYI, my use case is mostly to scope e.target to make typescript happy.

@vicky1999
Copy link
Contributor

Hi, I would like to work on this issue. can I try it?

@Conduitry
Copy link
Member

HTMLElement and SVGElement are both now marked as known globals in 3.42.2. Thanks @vicky1999!

@vicky1999
Copy link
Contributor

Thanks @Conduitry

@Yakumwamba
Copy link

Hi everyone, I have a question regarding HTMLElement. How can I set it as a global variable.

Thanks.

@elamje
Copy link

elamje commented Aug 7, 2022

I'm experiencing the same issue with HTMLCanvasElement now @Conduitry. I can put up a PR in a few minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug compiler Changes relating to the compiler good first issue A small, encapsulated issue, ideal for a new contributor to Svelte to tackle.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants