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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.