0

I am trying to get parameters of the electron dialog but I am getting two errors:

Type 'Dialog[U]' does not satisfy the constraint '(...args: any) => any'.

Type 'U' cannot be used to index type 'Dialog'.

I am doing this:

declare global {
  interface Window {
    dialogOpen: <T, U = keyof Electron.Dialog>(name: U, ...options: Parameters<Electron.Dialog[U]>) => Promise<T>;
  }
}

I then tried to manually type the value like this and I get no error.

type Test = Parameters<Electron.Dialog['showOpenDialog']>;

What am I doing wrong when I try to dynamically type the parameters?

1 Answer 1

1

Your problem is simply that the generic type parameter U merely defaults to keyof Electron.Dialog (using the X = Y notation); it is not at all constrained to keyof Electron.Dialog (using the X extends Y notation) . It is not constrained at all, and therefore Electron.Dialog[U] might not be a valid indexed access.

If you fix that:

interface Window {
  dialogOpen: <T, U extends keyof Electron.Dialog>(
    name: U, ...options: Parameters<Electron.Dialog[U]>) => Promise<T>;
}

then it compiles with no error. If you want you can keep the default also, like

interface Window {
  dialogOpen: <T, U extends keyof Electron.Dialog = keyof Electron.Dialog>(
    name: U, ...options: Parameters<Electron.Dialog[U]>) => Promise<T>;
}

depending on your use case.

Playground link to code

2
  • This works great for Classes, but errors with namespaces fs: <T, U extends keyof typeof fs = keyof typeof fs>( name: U, ...options: Parameters<keyof typeof fs[U]> ) => Promise<T>; in the Parameters utility. Any extra thoughts on that? Nov 20, 2022 at 1:07
  • If you could provide a minimal reproducible example or a link to one I might have thoughts on it, but ideally you'd make a new post if you have a followup question.
    – jcalz
    Nov 20, 2022 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.