microsoft / microsoft/TypeScript

Missing constraint error in instantiation expression for a nested class

Aperta
#61,982 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Variance Relationships Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔎 Search Terms

instantiation expression caching class expression declaration type arguments type parameter

🕗 Version & Regression Information
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250701#code/MYGwhgzhAECCA8AVaBeaA7ArgWwEYFMAnAPmgG8AoaaANzBE3wEIAuaRAbiumAAsBLEABNW0AJLoIAFzDpg+RAE8ADvnhSV+APYAzOADoAQvASJi5jtAD0V6FIEwIvLZmHQihLYQrdpYKfzA0IaoPOBQwUjuAB5S+OhCMLChsKSU1NTKYITxUqKc3AC+XIUUQA

💻 Code
class A<T = number> {
  value!: T;
  child!: InstanceType<typeof A.B<A<T>>>; // this should error

  static B = class B<T extends A = A> {
    parent!: T;
  };
}
🙁 Actual behavior

It doesn't error

🙂 Expected behavior

it should error with:

Type 'A<T>' does not satisfy the constraint 'A<number>'.
  Type 'T' is not assignable to type 'number'.(2344)

like it did in 5.6: TS playground

Additional information about the issue

The problem here is that the situation is a little bit circular and variances worker gets hits in reentrant manner. The nested call returns emptyArray that is a signal for structuredTypeRelatedToWorker to return Ternary.Unknown.

So checkTypeArguments called by getInstantiatedSignatures doesn't report an error. In the old version of the code, this would be re-called after the variance worker would completely exit and the error would be raised.

I see 2 ways to solve this:

  • ignore the introduced caching based inVarianceComputation to avoid spoiling this
  • defer checkTypeArguments in getInstantiatedSignatures (well, call it with /*reportErrors*/ false and defer anoher call with /*reportErrors*/ true)

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

Inizia con il repro collegato in TypeScript Playground e segui getInstantiatedSignatures attraverso checkTypeArguments, structuredTypeRelatedToWorker e il percorso inVarianceComputation. Confronta il comportamento con il link a Playground 5.6 e determina come il controllo differito o non memorizzato nella cache dovrebbe segnalare l'errore di vincolo. Il lavoro è completato quando il repro attuale produce la diagnostica 2344 senza compromettere il comportamento correlato del controllo dei tipi.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.