microsoft / microsoft/TypeScript

[Regression] Circular reference error when passing a class expression with non-primitive static properties to a generic function

Aperta
#59,271 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Circularity Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

class expression static type

🕗 Version & Regression Information
  • This changed between versions 4.0.5 and 4.1.5
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.1.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzg2RlQElUAHZDAHgBUA+ACi0uoC546BKTugbgBQgsHgDOGeFHgBeAiCIlyVDE0hQxY+AEF4Ab0EBICVGxgkOHLP0BfQTe5A

💻 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 = {}; }
})

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Iniziare con il TypeScript Playground collegato e confrontare la regressione tra le versioni 4.0.5 e 4.1.5 usando l’esempio della funzione generica e dell’espressione di classe. Verificare perché la proprietà statica non primitiva viene inferita come any e considerare completata l’issue quando l’esempio segnala A.foo come {} senza l’errore 7022, preservando il comportamento documentato del workaround.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.