microsoft / microsoft/TypeScript
Assertion function methods complains about missing annotation even with explicit annotation
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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:
Related Issues:
#36931
#37241
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das gemeldete Verhalten im verlinkten TypeScript Playground anhand des bereitgestellten Beispiels mit Pet, Base, Dog und Cat. Beginne damit nachzuverfolgen, wie der Compiler die explizite Annotation asserts this is Filter<Pet, T> der Assertion-Methode prüft, und vergleiche dies anschließend mit dem Fall des Klassentyps. Die Aufgabe ist abgeschlossen, wenn die Union-Annotation konsistent behandelt wird oder die Diagnose eindeutig erklärt, warum sie abgelehnt wird, und eine Regressionstestabdeckung in den relevanten Compiler-Tests hinzugefügt wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100