TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

onerror for Elements is incorrect

Open trusktr opened this issue 1 year ago • 3 comments

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:

  • https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event
  • https://udn.realityripple.com/docs/Web/API/GlobalEventHandlers/onerror

trusktr avatar Oct 08 '24 03:10 trusktr

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'>>
}

trusktr avatar Oct 08 '24 03:10 trusktr

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

saschanaz avatar Oct 09 '24 09:10 saschanaz

@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.

trusktr avatar Oct 11 '24 23:10 trusktr