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

ViteHotContext type should be customizable #16601

Closed
7 tasks done
FreeJ1nG opened this issue May 5, 2024 · 11 comments
Closed
7 tasks done

ViteHotContext type should be customizable #16601

FreeJ1nG opened this issue May 5, 2024 · 11 comments
Labels
documentation Improvements or additions to documentation

Comments

@FreeJ1nG
Copy link
Contributor

FreeJ1nG commented May 5, 2024

Describe the bug

I am trying to add custom events in addition to the current CustomEventMap, and i'd want the payload type of the custom events to be typed properly

// vite-env.d.ts (this line is not in the actual code)
/// <reference types="vite/client" />

interface CustomEventMap {
  'vite:customEvent': number;
}

export type InferCustomEventPayload<T extends string> =
  T extends keyof CustomEventMap ? CustomEventMap[T] : never;

interface ViteHotContext {
  on<T extends string>(
    event: T,
    cb: (payload: InferCustomEventPayload<T>) => void
  ): void;
}

interface ImportMetaEnv {
  readonly VITE_HI: string;
}

interface ImportMeta {
  readonly env: ImportMetaEnv;
  readonly hot?: ViteHotContext;
}
if (import.meta.hot) {
  import.meta.hot.on("vite:customEvent", (payload) => {});
                                             ^ expect to be number, got any
}

Reproduction

https://stackblitz.com/edit/vitejs-vite-pkygn6?file=src%2Fvite-env.d.ts

Steps to reproduce

npm install
npm run dev

System Info

System:
  OS: macOS 14.4.1
  CPU: (8) arm64 Apple M2
  Memory: 134.44 MB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 21.6.2 - /opt/homebrew/bin/node
  Yarn: 1.22.19 - /opt/homebrew/bin/yarn
  npm: 10.2.4 - /opt/homebrew/bin/npm
  pnpm: 9.0.6 - /opt/homebrew/bin/pnpm
  bun: 1.1.5 - ~/.bun/bin/bun
Browsers:
  Chrome: 124.0.6367.119
  Safari: 17.4.1
npmPackages:
  @vitejs/plugin-react-swc: ^3.5.0 => 3.6.0 
  vite: ^5.2.0 => 5.2.10

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented May 5, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@FreeJ1nG
Copy link
Contributor Author

FreeJ1nG commented May 5, 2024

I tried changing the vite source code for HMR types to not use imports at the top level, as suggested on TS' documentation.

I'm out of ideas on how to create the proper overridable types for the HMR declaration type

@bluwy
Copy link
Member

bluwy commented May 5, 2024

If you're adding custom events, have you tried this? https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events

It would be in a separate .d.ts file for it to work, which is understandably a bit odd compared to other typescript augmentations, but I think that pattern should also work.

@FreeJ1nG
Copy link
Contributor Author

FreeJ1nG commented May 5, 2024

If you're adding custom events, have you tried this? https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events

It would be in a separate .d.ts file for it to work, which is understandably a bit odd compared to other typescript augmentations, but I think that pattern should also work.

Hi Bjorn,

Thanks for the reply, I definitely missed that part of the docs! my bad

I tried following the example there, here is the code

UPD: I changed the stackblitz link since the previous link used an old vite version, but it still doesn't work properly

It still casts the payload to any, I'm not quite sure if I messed something up 😅

@sapphi-red
Copy link
Member

It seems it needs to be (notice the extension):

import 'vite/types/customEvent.d.ts';

declare module 'vite/types/customEvent.d.ts' {
  interface CustomEventMap {
    'custom:myCustomEvent': number;
  }
}

It seems we need the extension when "moduleResolution": "bundler" is used. I guess it's because we export it like this.

@FreeJ1nG
Copy link
Contributor Author

FreeJ1nG commented May 6, 2024

It seems it needs to be (notice the extension):

import 'vite/types/customEvent.d.ts';

declare module 'vite/types/customEvent.d.ts' {
  interface CustomEventMap {
    'custom:myCustomEvent': number;
  }
}

It seems we need the extension when "moduleResolution": "bundler" is used. I guess it's because we export it like this.

That definitely fixed it!

Shall I create a PR to fix the wording in the docs? or is it not necessary?

@sapphi-red
Copy link
Member

It would be great if you can do it 😀

@sapphi-red sapphi-red added documentation Improvements or additions to documentation and removed pending triage labels May 6, 2024
@FreeJ1nG
Copy link
Contributor Author

FreeJ1nG commented May 6, 2024

It would be great if you can do it 😀

Will do 🫡

I also think this part of the docs would be a better fit in the HMR API section rather than Plugin API, since it's a customization of the custom event type used in HMR

So here: https://vitejs.dev/guide/api-hmr.html#further-reading
Instead of: https://vitejs.dev/guide/api-plugin#typescript-for-custom-events

And maybe there could be a link in api-plugin referring to the one in api-hmr

What do you think about that?

@FreeJ1nG
Copy link
Contributor Author

FreeJ1nG commented May 6, 2024

Created a PR here

@jonlepage
Copy link

thanks guys

@sapphi-red
Copy link
Member

Closing as #16609 is merged.

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

No branches or pull requests

4 participants