microsoft / microsoft/TypeScript

TypeScript right adapt constructor

Open
#57,812 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs More Info
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides no repository file or test entry point; start by reproducing the JavaAdapter call with the overloaded android.widget.Button constructors shown in the example. Compare the accepted and rejected argument counts, then define a focused expected behavior for overload adaptation and add a regression test demonstrating what should be considered done.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.