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

[Feature Request]: Typescript list of Events #33410

Closed
3 tasks done
jdpaterson opened this issue Mar 23, 2022 · 2 comments
Closed
3 tasks done

[Feature Request]: Typescript list of Events #33410

jdpaterson opened this issue Mar 23, 2022 · 2 comments

Comments

@jdpaterson
Copy link

Preflight Checklist

Problem Description

Hi,

I am wondering if a similar issue or feature already exists. I am looking to create a Typescript type that accepts only valid electron events. For instance the BrowserWindow.on(event, listener) , I'm looking for a list of all of the valid events for .on. Something like

type AllBrowserWindowOnEvents = {
    'always-on-top-changed': (e: Event, isAlwaysOnTop: boolean) => void;
    'app-command: (e: Event, command: string) => void;
    ...and all of the other events, except I don't want to type them out
};

Does such a type already exist?

I tried things like:

type BrowserWindowOnEvents = Parameters<InstanceType<typeof Electron.BrowserWindow>['on']>[0];

but that only ever gives me Intellisense for the last BrowserWindow.on event, which is 'will-resize'.

It does work if I have new BrowserWindow().on(''), the intellisense will provide a list of all the possible events, but I'm looking to use it in a type definition when I don't have an instance of BrowserWindow.

Hope that makes sense, I would be willing to maybe put something together maybe for Electron-Userland but want to make sure nothing already exists.

Thanks

Proposed Solution

A type that lists all of the valid events for various listeners, ie

type AllBrowserWindowOnEvents = {
      'always-on-top-changed': (e: Event, isAlwaysOnTop: boolean) => void;
      'app-command: (e: Event, command: string) => void;
 }

Such a thing could be generated using the Typescript Compiler API

Alternatives Considered

Something that already exists that I'm missing.

Additional Information

No response

@vladimiry
Copy link

vladimiry commented Apr 7, 2022

but that only ever gives me Intellisense for the last BrowserWindow.on event, which is 'will-resize'.

TS doesn't support overloaded function args inferring, see microsoft/TypeScript#26591 (closed with won't fix status).

Proposed Solution

I'd like this to happen to workaround the declined microsoft/TypeScript#26591 TS's issue as I also have a need to narrow/extract the event args typing. But as far as I know TS declarations of @electron get generated from the docs which is not the same as manual typing.

@MarshallOfSound
Copy link
Member

Unfortunately due to how we generate our documentation we can either (a) leave things as they are which is great for intellisense and the way 99% of our users will consume the electron types or (b) change things to use generics and inference but in a way that let's TS code consume the event types as enums in a nice way.

Personally I don't see a good way of doing both and I think the obvious choice is to keep doing (a).

We publish electron-api.json with every release, if you want you can manually generate your own .d.ts file with custom enums / structs using that JSON api definition

@MarshallOfSound MarshallOfSound closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2022
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

3 participants