microsoft / microsoft/TypeScript
Type variable not inferred correctly unless unused declaration is provided
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
The described bug was noticed during debugging of the declarations provided with the Vue framework.
🔎 Search Terms
generic, inference, function, overload
🕗 Version & Regression Information
All 4.x versions available on the playground.
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Methods {
[key: string]: Function;
}
interface Param<M extends Methods> {
data: (this: M) => any;
methods: M;
}
declare function fun<M extends Methods = {}>(
param: Param<M> & ThisType<M>
): void;
// Uncommenting the declaration below results in proper type inference of M
// declare function fun<M extends Methods = {}>(
// param: { unusedProperty: true } & Param<M> & ThisType<M>
// ): void;
fun({
data() {
this.myMethod(); // Even after uncommenting the overloaded version, context is not properly inferred here
},
methods: {
myMethod() {},
mySecondMethod() {
this.myMethod(); // Error without uncommenting the declaration
}
},
});
🙁 Actual behavior
- Proper inference of the
Mtype variable andthiscontext in themethodssection depends on having an unused overloaded version of a function. - Even though
Mis properly inferred forfun(visible after hovering over the function) andthiscontext inmethodsafter uncommenting the unused declaration,thiscontext indatais still not properly inferred (when hovering overthis, it looks as it is inferred to the default{}).
🙂 Expected behavior
M type variable is properly inferred in both data function and methods section without redundant declarations.
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
Inizia eseguendo l'esempio collegato di TypeScript Playground e confronta l'inferenza per le due dichiarazioni di fun, concentrandoti sul generico M e sui contesti this in data e methods. Poiché nell'issue non sono identificati file del repository né test, individua prima i percorsi di inferenza del compilatore e di tipizzazione contestuale prima di aggiungere un caso di regressione; il lavoro è completato quando entrambi i contesti inferiscono M senza l'overload ridondante.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100