microsoft / microsoft/TypeScript
TypeScript right adapt constructor
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔍 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?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L’issue non fornisce alcun file del repository né un punto di ingresso per i test; inizia riproducendo la chiamata a JavaAdapter con i costruttori overloaded di android.widget.Button mostrati nell’esempio. Confronta il numero di argomenti accettati e rifiutati, quindi definisci un comportamento atteso mirato per l’adattamento degli overload e aggiungi un test di regressione che dimostri cosa dovrebbe essere considerato completato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100