microsoft / microsoft/TypeScript

this type in conditional type false clause is incorrectly typed

Abierto
#36,005 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: This-Typing
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce los ejemplos del TypeScript Playground enlazado, comparando el tipo contextual de this en f1, f2 y f3. Investiga la tipificación contextual de tipos condicionales para la cláusula false; se considera terminado cuando el this del callback de f3 tiene el tipo { b: 1 } en lugar del tipo base ensanchado, sin afectar regresivamente a los otros ejemplos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.