microsoft / microsoft/TypeScript

Assertion function methods complains about missing annotation even with explicit annotation

Open
#39,361 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported behavior in the linked TypeScript Playground using the provided Pet, Base, Dog, and Cat example. Start by tracing how the compiler checks the assertion method's explicit asserts this is Filter<Pet, T> annotation, then compare it with the class-type case. Done means the union annotation is handled consistently or the diagnostic clearly explains why it is rejected, with regression coverage added in the relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.