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

Declare missing vite:preloadErrorevent on Window namespace #17508

Open
4 tasks done
MathiasWP opened this issue Jun 18, 2024 · 0 comments
Open
4 tasks done

Declare missing vite:preloadErrorevent on Window namespace #17508

MathiasWP opened this issue Jun 18, 2024 · 0 comments

Comments

@MathiasWP
Copy link

Description

Vite has a vite:preloadError custom event which can be used when loading a dynamic import fails.

This event is not declared in the global namespace, so doing

window.addEventListener('vite:preloadError', (event) => {
  event.payload // Property 'payload' does not exist on type 'Event'.
})

or

//   Argument of type '(event: Event & { payload: Error; }) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject
window.addEventListener('vite:preloadError', (event: Event & { payload: Error }) => {
  event.payload 
})

will not work unless you override the event type.

Suggested solution

Declare the event in a global namespace:

declare interface VitePreloadErrorEvent extends Event {
	payload: Error;
}

declare interface Window {
	addEventListener(
		type: 'vite:preloadError',
		listener: (this: Window, ev: VitePreloadErrorEvent) => unknown,
		options?: boolean | AddEventListenerOptions
	): void;
	removeEventListener(
		type: 'vite:preloadError',
		listener: (this: Window, ev: VitePreloadErrorEvent) => unknown,
		options?: boolean | AddEventListenerOptions
	): void;
}

Alternative

Adding this declaration to my own vite.d.ts file, but it feels like something Vite should export since the library knows the exact type.

Additional context

No response

Validations

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

No branches or pull requests

1 participant