microsoft / microsoft/TypeScript

Private/protected properties on siblings in union types

Abierto
#9,974 3 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: Error Messages Help Wanted Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

TypeScript Version: 1.8.9

Code

class Example {
    protected width = 0;
    constructor(sibling: Example | HTMLCanvasElement) {
        console.log(sibling.width);
    }
}

Expected behavior: No errors.

Actual behavior: Error: Property 'width' does not exist on type 'Example | HTMLCanvasElement'. Line 4, column 31

The reason this error occurs is that the width property is protected on Example but public on HTMLCanvasElement. However, the error is confusing for two reasons:

  1. The width property is available on both component properties in this situation. If the sibling parameter had a type of either Example or HTMLCanvasElement, the code would compile.
  2. The error message doesn't explain why the property does not exist on the type. Understanding the error requires the knowledge that properties are only available on union types if they have the same privacy level, even if those properties would be accessible in the component types in the active context.

I am not sure if there are situations in which the desired behavior is a compilation error when accessing a potentially protected property on a union type if that property is accessible on all component types. I can't think of any, and so I think the ideal situation would be that the compiler does not throw an error here. However, I think it would be an adequate solution to improve the error message.

(I apologize if this has been reported/discussed before. I searched but couldn't find anything exactly along these lines.)

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

Comienza compilando el ejemplo de TypeScript del issue con TypeScript 1.8.9 y confirma el diagnóstico de la propiedad de unión. Sigue la ruta del checker para acceder a width en Example | HTMLCanvasElement y determina después si los miembros protected/public accesibles deben compilarse o recibir un error más claro. La tarea está terminada cuando el comportamiento elegido está implementado y cubierto por una prueba de regresión para este ejemplo.

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.