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?