microsoft / microsoft/TypeScript
this type in conditional type false clause is incorrectly typed
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.7.3
Search Terms:
generic type this widening incorrect
Code
type Option1 = { run(this: { b: 1 }): void }
function f1(options: Option1) { return options }
// `this` is correctly typed as `{ b: 1 }`
f1({ run() { this.b } })
type Option2<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(arg: { b: 1 }): void
}
function f2<Config>(options: Option2<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f2({ config: { a: 1 }, run() { this.a } })
// `arg` is correctly typed as `{ b: 1 }`
f2({ run(arg) { arg.b } })
type Option3<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(this: { b: 1 }): void
}
function f3<Config>(options: Option3<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f3({ config: { a: 1 }, run() { this.a } })
// `this` is widen to what `Config` extends to. In this case `Record<string, any>`
f3({ run() { this.b } })
Expected behavior:
this in f3({ run() { this.b } }) should be typed as in this in f1() and arg in f2()
Actual behavior:
this in f3({ run() { this.b } }) is typed as the base type of Config (Record<string,any> in the example above)
Playground Link:
Playground Link
Related Issues:
Some maybe related issues:
https://github.com/microsoft/TypeScript/issues/32990
https://github.com/microsoft/TypeScript/issues/30152
https://github.com/microsoft/TypeScript/issues/13995
I also recall there were some discussion about type and Record<>, but that is in the gitter channel and I can't find relevant issue or doc about it.
I think this is different than the related issues above because this does not involve union type.
This is about the false condition of the conditional type Config extends Record<string, any> ? ... : ... do not use the generic type Config and should not be affected by it.
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
Riproduci gli esempi nel TypeScript Playground collegato, confrontando il tipo contestuale di this in f1, f2 e f3. Analizza la tipizzazione contestuale dei tipi condizionali per la clausola false; il lavoro è completo quando il this del callback di f3 è tipizzato come { b: 1 } invece che come il tipo base ampliato, senza introdurre regressioni negli altri esempi.
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