microsoft / microsoft/TypeScript
TypeScript right adapt constructor
Nobody has claimed this yet.
- 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
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
TypeScript matches correctly based on parameters
📃 Motivating Example
Enhance the user experience of typescript
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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