microsoft / microsoft/TypeScript
Support either type asserting `self` in classes, or properly ignoring workaround
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
I currently have a scenario very similar to https://github.com/microsoft/TypeScript/issues/2000#issuecomment-546929745. While it would be fixed it TS natively supported overriding class methods (see that issue), I followed the outline of the linked comment, but am running into a small issue. This is not a huge blocker but rather a nice-to have to get proper type completion for my scenario.
export class WritableObjStream<T> extends (stream.Writable as any) {
writableObjectMode: true
write(object: T, cb?: (error: Error | null | undefined) => void): boolean {
return super.write(...more details)
}
}
Because we type assert the base class to any, TS has no knowledge of the superclass. I tried a few solutions, with errors included below:
(super as any).write() // 'super' must be followed by an argument list or member access.
let s: stream.Writable = undefined;
// @ts-ignore
s = super // Identifier expected.
return s.write() // 'super' must be followed by an argument list or member access, Expected 1-3 arguments, but got 0.
Luckily, it autocomplete is picking up on the proper type of s.write() now and giving me an argument error.
Also, from what I can gather super is treated as a keyword not an identifier, which results in the error above.
This may not be the most common of errors, but there may be a few scenarios/other use cases. I tried writing a few examples up but none made real-world sense besides this use-case of a workaround for overriding methods.
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 el ejemplo de clase del issue y con el issue/comentario enlazado de TypeScript que describe la solución alternativa. Investiga cómo TypeScript analiza y comprueba los tipos de super en este escenario, y determina después si debería admitirse la aserción de tipo de self o si la solución alternativa debería rechazarse explícitamente. Se considera terminado cuando el comportamiento elegido está documentado mediante pruebas que cubren los ejemplos mostrados.
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