microsoft / microsoft/TypeScript

Assertion function methods complains about missing annotation even with explicit annotation

Aperta
#39,361 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

This may be working as intended but the error message is at least unclear for this case. It's not obvious why explicit annotation with a Union is different than explicit annotation with a class type.

TypeScript Version: 3.9.4

Search Terms:

  • assertion function union
  • assertion function

Code

type Pet = Cat | Dog;

type Filter<V extends Pet, T extends 'dog' | 'cat' > = V extends { type: T } ? V : never

abstract class Base {
    abstract type: 'dog' | 'cat'   
  
    assert<T extends 'dog' | 'cat'>(this: Pet, type: T): asserts this is Filter<Pet, T>  {
        if (this.type === type) return
        throw new Error('nope')
    }
}

class Dog extends Base {
    type = 'dog' as const
    bark() { }
}       
class Cat extends Base  {
    type = 'cat' as const
    meow() { }
}

const a: Pet = new Dog() as any

const b: Dog = new Dog() as any

a.assert('dog')
a.bark()

b.assert('dog')
b.bark()

Expected behavior:

a.assert() to narrow the type and not complain about an explicit type annotation

Actual behavior:

a.asserts is marked as implicitly typed

Playground Link:

https://www.typescriptlang.org/play/index.html?ssl=38&ssc=9&pln=3&pc=1#code/LAKFBcE8AcFMAIAKtzwLzwMIENUB94ARAewHMBuUCGBAMQEsAbcWAJwB4A1eWADxYB2AEwDOSFABp4AFR79YwsQHIhZJfAJKAxrnUA+dPG59Bo+AG94UOAC4Z8AL7wA-Efh2BsAG5sqIbABGIuCs2FqoWozYImIAQtEI5qDwKfCBwaHhVjR2Kmoa8Nq6qcnwpSnRImzg7LImCmZ5pOqaOuBKegAU4AAW9CJ2yOBS1rB20gCUdpXVYr398AsMzGzsQ1LSBhblqSn0AGbw3X0iAHSj6GgYoxPwrCgArqwCO7u9rMQA7vCe3wCirA+rE6SgExDgSgmOwcoBhYBA+weAnC9GIAjSMWq0hotTkpmUqmaBSK7S6XkGkmythkUwxVVY4DEXkWYmWLA46xkWySIF2ByOXnONEu1xot3u4CeL15qXeXx+sH+gOIwNB4NgkNhfkilSIZDxDTiCW2MpSFwwTXU0XgWjRwR2AWwrAA1p1bpY4U5dilQDqYlhcAbFPB4lUUjy3sKLW0rWJbQJ7ab4ABbWBfN0WRxa+G+u2obAU1AYX560gZ63YASQXMJ1ABOwkUiGEuN8tiSvV+HYU4zBkgwma-ynR0ut1+AI9zF9y1QkATkeu2dAA

Related Issues:

#36931
#37241

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

Riproduci il comportamento segnalato nel TypeScript Playground collegato usando l'esempio fornito di Pet, Base, Dog e Cat. Inizia tracciando il modo in cui il compilatore verifica l'annotazione esplicita asserts this is Filter<Pet, T> del metodo di asserzione, quindi confrontalo con il caso del tipo di classe. Il lavoro è completato quando l'annotazione di unione viene gestita in modo coerente oppure la diagnostica spiega chiaramente perché viene rifiutata, e viene aggiunta una copertura di regressione nei test pertinenti del compilatore.

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.