microsoft / microsoft/TypeScript
[Regression] Circular reference error when passing a class expression with non-primitive static properties to a generic function
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
🔎 Search Terms
class expression static type
🕗 Version & Regression Information
- This changed between versions 4.0.5 and 4.1.5
⏯ Playground Link
💻 Code
declare function returnInput<T>(input: T): T;
const a = returnInput(class A {
static foo = {}
// ^?
})
🙁 Actual behavior
A.foo: any
Error: 'foo' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
And also note that this bug only happens when the type of foo is not a primitive type.
🙂 Expected behavior
A.foo: {}, as it works in 4.0.5
Additional information about the issue
To define a React class component, it is a common practice to include its "sub-components" as private static properties. I also use a "observer" function to make the state change auto observed. So the real-world code may look like:
import { observer } from 'mobx-react';
import { styled } from 'styled-components';
@observer
class ListView extends React.Component {
// render method here...
private static readonly Item = observer(class Item extends React.Component {
private static readonly Header = styled.h4` // <- Cannot infer the type of Header
// CSS style here...
`;
})
}
A workaround is to use static block to initialize the static property but it's cumbersome:
const a = returnInput(class A {
static foo
// ^? OK
static { this.foo = {}; }
})
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
Comience con el TypeScript Playground enlazado y compare la regresión entre las versiones 4.0.5 y 4.1.5 usando el ejemplo de la función genérica y la expresión de clase. Investigue por qué la propiedad estática no primitiva se infiere como any y considere terminado el issue cuando el ejemplo informe A.foo como {} sin el error 7022, preservando el comportamiento documentado del workaround.
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
- Bien especificado
- Aptitud para principiantes
- 45/100