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

Closed
4 tasks done
MathiasWP opened this issue Jun 18, 2024 · 1 comment · Fixed by #17615
Closed
4 tasks done

Declare missing vite:preloadErrorevent on Window namespace #17508

MathiasWP opened this issue Jun 18, 2024 · 1 comment · Fixed by #17615
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)

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

@patak-dev
Copy link
Member

This sounds good to me. Would you like to do a PR?

@patak-dev patak-dev added enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Jul 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants