microsoft / microsoft/TypeScript

ES6 Class: `asserts this is Type` raises error when variable containing instance is type narrowed

Aperta
#54,591 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: This-Typing Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms
  • Error: "Assertions require every name in the call target to be declared with an explicit type annotation"
  • Assertation function as class method
  • Type narrowing class and then calling assertation method
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link

Playground link with relevant code

💻 Code
class MyClass {
	public assertIsMyClass(): asserts this is MyClass {
		if (!(this instanceof MyClass)) {
			throw new Error('this was not of type MyClass');
		}
	}
}

const maybeMyClass: MyClass | undefined = new MyClass();

if (maybeMyClass) {
	// This fails with error:
	// Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
	maybeMyClass.assertIsMyClass();

	// But if we simply assign to a new variable without the optionality, it's fine:

	// this line works because we have narrowed the type through the if statement above
	const definitelyMyClass: MyClass = maybeMyClass; 
	// This raises no error even though it's equivalent to the first attempt
	definitelyMyClass.assertIsMyClass();
}
🙁 Actual behavior

Calling the class method assertation function raises error: "Assertions require every name in the call target to be declared with an explicit type annotation.(2775)"

🙂 Expected behavior

It should pass validation and narrow to the type given in the assertation.
This is because it will have this behavior if you simply assign to a new variable with a new type annotation (but which is equivalent to the narrowed type at that point).

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 link Playground fornito e riproduci la diagnostica nell’esempio di codice, confrontando la variabile il cui tipo è stato ristretto con la variabile annotata esplicitamente. Segui la convalida delle chiamate di asserzione nel compilatore TypeScript e aggiungi la copertura per questo caso di metodo di classe. Il lavoro è completato quando la chiamata con il tipo ristretto supera la convalida e i controlli di asserzione esistenti rimangono corretti.

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.