microsoft / microsoft/TypeScript
This-function parameters must be specified in caller *and* callee, even for methods
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
This means that only very careful people will benefit from assignability checking (and therefore argument checking at call sites). (Due to contextual typing, code that uses a this-typed library will get improved body checking a lot of the time.)
Specifically, assignability checking only happens when this is explicitly declared by both sides.
Instead, class methods should implicitly have this: this unless declared otherwise.
Given the declarations:
declare let f: (this: string, x: number) => void;
declare class C {
m(x: number): void;
n(this: this, x: number): void;
}
declare let c: C;
Expected behavior:
c.m = f // error, 'this: string' is not assignable to 'this: string'
f = c.m // error, 'this: C' is not assignable to 'this: string'
f = c.n // error, 'this: string' is not assignable to 'this: C'
c.n = f // error, 'this: C' is not assignable to 'this: string'
Actual behavior:
c.m = f // no error
f = c.m // no error
f = c.n // error, 'this: string' is not assignable to 'this: C'
c.n = f // error, 'this: C' is not assignable to 'this: string'
Comments
The "this parameter" feature was designed this way to balance four concerns:
- Backward compatibility with un-annotated code.
- Compiler efficiency.
- Consistency between class and interface
this. - Quality of error checking.
As you can see, (4) took the biggest hit by requiring annotation on both the source and target of the assignment.
Backward compatibility
The problem with unannotated code is that we can't tell whether a function is intended to be called with or without this.
Functions
--noImplicitThis helps address this problem by forcing annotation of this parameters for functions.
TODO: Work through some examples.
Methods
Actually, for methods, we do have a pretty good idea that a method is not supposed to be removed from its class. Even if the class implements an interface, we the class methods should still be able to refer to class-private implementation details.
TODO: Work through some examples.
Interface consistency
The problem is that interfaces are frequently used to interact with objects instead of the class type itself. But interfaces are used in lots of other contexts as well. That means we can't infer intent with an interface, but if we don't add this: this to interface methods, we're left with a bad assignment problem:
interface I {
m(x: number): void;
}
class C implements I {
y = 12;
m(x: number) {
return this.y + x;
}
}
let c = new C();
f = c.m;
f(); // error, this must of type 'C'
let i: I = c;
f = i.m;
f(); // ok, this is not annotated
"Assignment escape hatches" like this exist throughout TypeScript, but adding more is not a good thing.
Efficiency
Briefly, if every interface has a this type, then every interface will be generic, even in non-OO code. This causes the compiler to generate about double the number of types internally, which is slower and uses more memory than today.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de asignabilidad del issue y rastrea cómo se comprueban los parámetros this explícitos para métodos, clases e interfaces. Un cambio completo produciría los cuatro errores esperados, teniendo en cuenta la compatibilidad con versiones anteriores, la coherencia de las interfaces, la eficiencia del compilador y los ejemplos no resueltos marcados con TODO.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100