microsoft / microsoft/TypeScript
Inference isn't working with constructor that contextually types 'this' with intersections of itself
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TL;DR: There should be no errors in the below example.
I am trying to model a library that takes an options bag for its constructor, and exposes those properties onto the instance that is eventually produced.
For example, if I wrote
let instance = new Component({
methods: {
hello() { }
world() {
this.hello();
}
}
});
I should be able to write instance.hello() or instance.world().
Additionally, since this in world will be bound to the instance, this.hello should be legal and well-typed in the above example.
However, I'm not able to come up with a version of this that works.
/**
* Ensures all properties have a `this` param of type `T`.
*/
interface ThisEnforced<T> {
[prop: string]: (this: T, ...args: any[]) => any;
}
interface ComponentOptions<Methods> {
methods?: Methods
}
interface Component<Methods extends ThisEnforced<Component<Methods> & Methods>> {
$methods: Methods;
}
interface ComponentStatic {
new <Methods extends ThisEnforced<Component<Methods> & Methods>>(options: ComponentOptions<Methods>):
// Returns...
Component<Methods> & Methods;
}
declare var Component: ComponentStatic;
let inst = new Component({
methods: {
hello() {
// ...
},
world() {
this.hello;
// ~~~~~
// Error: Property `hello` does not exist on this type.
}
},
});
inst.hello;
// ~~~~~
// Error: Property `hello` does not exist on this type.
In the above example, there should be no errors - however, it appears that no inferences are being drawn as a type argument for Methods, and both this.hello and inst.hello are causing errors.
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 riproducendo l’esempio di tipizzazione contestuale e inferenza del costruttore nell’issue, concentrandoti sul tipo this all’interno di world e sul tipo inferito di inst. Traccia i percorsi di controllo dei tipi e di inferenza generica di TypeScript coinvolti. Il lavoro è completo quando l’esempio non produce errori, this.hello viene accettato e inst.hello è disponibile con una tipizzazione appropriata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 25/100