microsoft / microsoft/TypeScript
Flag 'instanceof' expressions that are provably always true or false
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Suggestion
When refactoring code, TypeScript is generally very good at finding and reporting situations where existing code will break due to type changes. This allows "refactoring with confidence". This isn't surprising given that one of the primary goals of TypeScript is to "statically identify constructs that are likely to be errors".
There's one common case where TypeScript is "blind" and doesn't report errors when types are refactored. This case involves the use of the 'instanceof' operator. This operator can be used in situations where it makes no sense -- e.g. where the type specified on the LHS has no possible relation to the type specified on the RHS.
To help developers avoid this common programming error, I propose that TypeScript report an error for any 'instanceof' expression that is provably always true or false at compilation time. Any such expression will likely be unintended by the programmer and should be flagged as errors. At best, such operations represent unnecessary code that imposes runtime overhead.
Example
// ClassB is a subclass of ClassA.
class ClassA {}
class ClassB extends ClassA {}
// ClassC has nothing to do with either
// ClassA or ClassB.
class ClassC {
method1() {}
}
function function1(param1: ClassB) {
// Static analysis can prove this expression
// will always evaluate to false. It's a common
// source of programming errors, especially during
// refactoring.
if (param1 instanceof ClassC) {
// This call isn't even valid given that param1
// was declared as type ClassB.
param1.method1();
}
// Static analysis can prove this expression will
// always evaluate to true, so at best it's unnecessary
// runtime overhead and more commonly is a behavior
// that is unintended by the programmer.
if (param1 instanceof ClassA) {
// ...
}
}
Are there any legitimate uses of 'instanceof' where the result is statically provable to be true or false in all cases? I can't think of any, but it's possible I'm overlooking some specialized cases. If there are, this check could be added as an optional compiler switch to preserve the current behavior.
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 revisando los ejemplos de ClassA, ClassB y ClassC del issue y el comportamiento existente de comprobación de tipos con instanceof en el compilador de TypeScript. Determina cómo deben diagnosticarse los casos siempre verdaderos y siempre falsos, incluidas las excepciones legítimas. Se considera terminado cuando el compilador informa de los casos previstos, existe cobertura para los ejemplos y se conserva el uso válido de instanceof.
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
- 28/100