Skip to content

TypeScript right adapt constructor #57812

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

Open
6 tasks done
little-alei opened this issue Mar 17, 2024 · 2 comments
Open
6 tasks done

TypeScript right adapt constructor #57812

little-alei opened this issue Mar 17, 2024 · 2 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@little-alei
Copy link

🔍 Search Terms

I have implemented a function JavaAdapter to inherit classes and return instantiated objects. Now I'm creating a declaration for it.

type ProxyMethods<T extends object> = { [K in keyof T]?: T[K] } | {};

declare function JavaAdapter<T, K extends ProxyMethods<T>, Args>(
  className: new (...args: Args) => T,
  override: K,
  ...args: Args
): K & T;

In most cases, it works fine. However, when the inherited object has multiple constructor functions with different parameter counts, it cannot accurately match, for example:

JavaAdapter(
  android.widget.Button,
  {
    setOnClickListener(view) {
      console.log(view);
    },
  },
  activity,
  // new android.util.AttributeSet()
);

This usage is correct and matches one of the constructor functions, but TypeScript prompts "Expected 4 arguments, but got 3." It works only when I add new android.util.AttributeSet(). The partial declaration of android.widget.Button is as follows:

declare module android {
  export module widget {
    class Button {
      public setOnClickListener(view: this): void;
      public constructor(context: android.content.Context);
      public constructor(context: android.content.Context, attribute: android.util.AttributeSet);
    }
  }
}

Besides changing to constructor(context: android.content.Context, attribute?: android.util.AttributeSet);, can TypeScript better adapt?

✅ Viability Checklist

⭐ Suggestion

TypeScript matches correctly based on parameters

📃 Motivating Example

Enhance the user experience of typescript

💻 Use Cases

  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?
@jcalz
Copy link
Contributor

jcalz commented Mar 17, 2024

Looks like a duplicate of #35501 (marked “closed” but not actually fixed) or one of the similar issues related to it.

@RyanCavanaugh
Copy link
Member

The repro has a bunch of unbound identifiers, can you post something that's self-contained?

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants