microsoft / microsoft/TypeScript
this type in conditional type false clause is incorrectly typed
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Reproduisez les exemples dans le TypeScript Playground lié, en comparant le type contextuel de this dans f1, f2 et f3. Étudiez le typage contextuel des types conditionnels pour la clause false ; le travail est terminé lorsque le this du callback de f3 est typé comme { b: 1 } plutôt que comme le type de base élargi, sans entraîner de régression dans les autres exemples.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100