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

onerror for Elements is incorrect #1821

Open
trusktr opened this issue Oct 8, 2024 · 3 comments
Open

onerror for Elements is incorrect #1821

trusktr opened this issue Oct 8, 2024 · 3 comments

Comments

@trusktr
Copy link

trusktr commented Oct 8, 2024

const img = document.createElement('img')

// type error:
img.onerror = (e: Event) => e
Screenshot 2024-10-07 at 8 03 39 PM

playground

Only Window.onerror should have the alternative signature, but not elements. See:

@trusktr
Copy link
Author

trusktr commented Oct 8, 2024

The situation I ran into is that trying to create JSX types for Custom Elements, it is leaking into the JSX types, causing unexpected errors:

Screenshot 2024-10-07 at 8 06 21 PM

| string fixes the error, but is not accurate:

Screenshot 2024-10-07 at 8 07 42 PM

To work around the issue in JSX types, we can override it like so (differs slightly depending on the JSX from Solid, Preact, React, etc):

interface IntrinsicElements {
	'some-element': Omit<JSX.HTMLAttributes<SomeElement>, 'onerror'>
		& { onerror?: ((error: ErrorEvent) => void) | null }
		& Partial<Pick<SomeElement, 'foo', 'bar'>>
}

@saschanaz
Copy link
Contributor

This is a bit awkward situation as img inherits from GlobalEventHandlers. I wonder we can somehow override it?

@trusktr
Copy link
Author

trusktr commented Oct 11, 2024

@saschanaz I ran this code,

const img = document.createElement('img')
img.onerror = (...args) => console.log(args.length, ...args)
img.src = '@#$%'

and it logs an 1, Event (not 1, ErrorEvent) in Chrome, Edge, and Firefox, and Safari.

Hmm, maybe the parameter type needs to be just Event, but it still doesn't need the signature of GlobalEventHandlers.onerror.

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

No branches or pull requests

2 participants